跳转至

parentPort

用于与父进程通信的接口。

进程:Utility

parentPort 是一个 EventEmitter。 此对象未从 'electron' 模块导出。它仅作为 Electron API 中 process 对象的属性可用。

// 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 任意
  • ports MessagePortMain[]

当进程接收到消息时发出。在此端口上接收到的消息将被排队,直到为此事件注册了处理程序。

方法

parentPort.postMessage(message)

  • message 任意

从进程向其父进程发送消息。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron