跳转至

parentPort

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

进程:Utility

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

// 主进程
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' })
child.on('message', (data) => {
  console.log(data) // 你好,世界!
})

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

事件

parentPort 对象会发出以下事件:

事件:'message'

返回:

  • messageEvent Object
    • data any
    • ports MessagePortMain[]

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

方法

parentPort.postMessage(message)

  • message any

从当前进程向其父进程发送一条消息。

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