跳到主要內容

WebContentsView

一個顯示 WebContents 的 View。

進程:主進程

此模組必須在 app 模組的 ready 事件發射後才能使用。

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.dev.org.tw')
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

一個顯示 WebContents 的 View。

進程:主進程

WebContentsView 繼承自 View

WebContentsView 是一個 EventEmitter

new WebContentsView([options])

  • options 物件 (選用)
    • webPreferences WebPreferences (選用) - 網頁功能設定。
    • webContents WebContents (選用) - 如果存在,給定的 WebContents 將被 WebContentsView 採用。一個 WebContents 一次只能在一個 WebContentsView 中呈現。

建立一個 WebContentsView。

實例屬性

使用 new WebContentsView 建立的物件除了繼承自 View 的屬性外,還具有以下屬性

view.webContents 唯讀

一個 WebContents 屬性,包含對顯示的 WebContents 的參考。使用此屬性與 WebContents 互動,例如載入 URL。

const { WebContentsView } = require('electron')
const view = new WebContentsView()
view.webContents.loadURL('https://electron.dev.org.tw/')