Skip to main content

WebContentsView

显示 WebContents 的视图。

¥A View that displays a 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 的视图。

¥A View that displays a WebContents.

进程:主进程

¥Process: Main

WebContentsView 继承自 View

¥WebContentsView inherits from View.

WebContentsViewEventEmitter

¥WebContentsView is an EventEmitter.

new WebContentsView([options])

  • options 对象(可选)

    ¥options Object (optional)

    • webPreferences WebPreferences(可选) - 网页功能的设置。

      ¥webPreferences WebPreferences (optional) - Settings of web page's features.

    • webContents WebContents(可选) - 如果存在,给定的 WebContents 将由 WebContentsView 采用。WebContents 一次只能在一个 WebContentsView 中显示。

      ¥webContents WebContents (optional) - If present, the given WebContents will be adopted by the WebContentsView. A WebContents may only be presented in one WebContentsView at a time.

创建 WebContentsView。

¥Creates a WebContentsView.

实例属性

¥Instance Properties

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

¥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/')