跳至主要內容

parentPort

與父程序通訊的介面。

程序:工具

parentPort 是一個 EventEmitter此物件不是從 'electron' 模組匯出的。它僅作為 Electron API 中程序物件的屬性可用。

// Main process
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' })
child.on('message', (data) => {
console.log(data) // hello world!
})

// Child process
process.parentPort.on('message', (e) => {
process.parentPort.postMessage(`${e.data} world!`)
})

事件

parentPort 物件會發出以下事件

事件:'message'

回傳

  • messageEvent 物件
    • data any
    • ports MessagePortMain[]

當程序收到訊息時發出。在此埠接收到的訊息將會排隊,直到為此事件註冊處理常式。

方法

parentPort.postMessage(message)

  • message any

從程序傳送訊息到其父程序。