Skip to main content

WebContentsView

显示 WebContents 的视图。

进程:主进程

🌐 Process: Main

app模块的ready事件被触发之前,该模块无法使用。

🌐 This module cannot be used until the ready event of the app module is emitted.

const { BaseWindow, WebContentsView } = require('electron')

const win = new BaseWindow({ width: 800, height: 400 })

const view1 = new WebContentsView()
win.contentView.addChildView(view1)
view1.webContents.loadURL('https://electron.nodejs.cn')
view1.setBounds({ x: 0, y: 0, width: 400, height: 400 })

const view2 = new WebContentsView()
win.contentView.addChildView(view2)
view2.webContents.loadURL('https://github.com/electron/electron')
view2.setBounds({ x: 400, y: 0, width: 400, height: 400 })

类:WebContentsView 继承 View

🌐 Class: WebContentsView extends View

显示 WebContents 的视图。

进程:主进程

🌐 Process: Main

WebContentsView 继承自 View

WebContentsView 是一个 事件触发器

warning

Electron 内置的类不能在用户代码中被继承。 欲了解更多信息,请参见 常见问题

new WebContentsView([options])

  • options 对象(可选)
    • webPreferences WebPreferences(可选)- 网页功能的设置。
    • webContents WebContents(可选)- 如果提供,指定的 WebContents 将被 WebContentsView 采用。一个 WebContents 在同一时间只能出现在一个 WebContentsView 中。

创建 WebContentsView。

🌐 Creates a WebContentsView.

实例属性

🌐 Instance Properties

使用 new WebContentsView 创建的对象除了继承自 View 的属性外,还具有以下属性:

🌐 Objects created with new WebContentsView have the following properties, in addition to those inherited from View:

view.webContents 只读

🌐 view.webContents Readonly

WebContents 属性包含对显示的 WebContents 的引用。
使用它可以与 WebContents 交互,例如加载 URL。

🌐 A WebContents property containing a reference to the displayed WebContents. Use this to interact with the WebContents, for instance to load a URL.

const { WebContentsView } = require('electron')

const view = new WebContentsView()
view.webContents.loadURL('https://electron.nodejs.cn/')