parentPort
与父进程通信的接口。
进程:Utility
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任意portsMessagePortMain[]
当进程接收到消息时发出。在此端口接收的消息将被排队,直到为此事件注册了处理程序。
方法
parentPort.postMessage(message)
messageany
将消息从进程发送到其父进程。