BrowserView

类: BrowserView

创建和控制视图

进程:主进程

BrowserView 被用来让 BrowserWindow 嵌入更多的 web 内容。 它就像一个子窗口,除了它的位置是相对于父窗口。 这意味着可以替代webview标签.

示例

// 在主进程中.const { BrowserView, BrowserWindow } = require('electron')let win = new BrowserWindow({ width: 800, height: 600 })win.on('closed', () => {  win = null})let view = new BrowserView()win.setBrowserView(view)view.setBounds({ x: 0, y: 0, width: 300, height: 300 })view.webContents.loadURL('https://electronjs.org')

new BrowserView([可选]) 实验功能

  • options Object (optional)

  • webPreferences Object (可选) - 详情请看 BrowserWindow.

静态方法

BrowserView.getAllViews()

返回 BrowserView[] - 所有打开的 BrowserViews 的数组。

BrowserView.fromWebContents(webContents)

  • webContents WebContents

返回 BrowserView | null - 如果内容不属于BrowserView,则它拥有返回的webContents或null。

BrowserView.fromId(id)

  • id Integer

返回 BrowserView - 带有id的视图.

实例属性

使用 new BrowserView 创建的对象具有以下属性:

view.webContents 实验功能

视图的WebContents 对象

view.id 实验功能

视图的唯一ID Integer.

实例方法

使用 new BrowserView创建的对象具有以下实例方法:

view.destroy()

强制关闭视图, 不会为网页发出 unload 和 beforeunload 事件。 完成视图后, 请调用此函数, 以便尽快释放内存和其他资源。

view.isDestroyed()

返回 Boolean -判断窗口是否被销毁

view.setAutoResize(options) 实验功能

  • options Object

  • width Boolean (optional) - If true, the view’s width will grow and shrink together with the window. false by default.

  • height Boolean (optional) - If true, the view’s height will grow and shrink together with the window. false by default.

  • horizontal Boolean (optional) - If true, the view’s x position and width will grow and shrink proportionally with the window. false by default.

  • vertical Boolean (optional) - If true, the view’s y position and height will grow and shrink proportionally with the window. false by default.

view.setBounds(bounds) 实验功能

  • bounds Rectangle

调整视图的大小,并将它移动到窗口边界

view.getBounds() 实验功能

返回 Rectangle

The bounds of this BrowserView instance as Object.

view.setBackgroundColor(color) 实验功能

  • color String - Color in #aarrggbb or #argb form. The alpha channel is optional.