类: BrowserView

创建和控制视图

进程:主进程

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

示例

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

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

  • 参数 Object (可选)

静态方法

BrowserView.getAllViews()

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

BrowserView.fromWebContents(webContents)

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

BrowserView.fromId(id)

  • id Integer

返回 BrowserView - 带有id的视图.

实例属性

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

view.webContents 实验功能

视图的WebContents 对象

view.id 实验功能

视图的唯一ID Integer.

实例方法

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

view.destroy()

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

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) 实验功能

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

view.getBounds() 实验功能

返回 Rectangle

The bounds of this BrowserView instance as Object.

view.setBackgroundColor(color) 实验功能

  • color String - 颜色值格式为 #aarrggbb#argb, 透明度为可选参数.