@2o3t/electron-ipc-promise 中文文档教程

发布于 5年前 浏览 20 项目主页 更新于 3年前

@2o3t/electron-ipc-promise

Installation

yarn add @2o3t/electron-ipc-promise

Usage

// in main process
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc.on('notify', (newSettings) => {
    console.log(newSettings);
    return 'test';
});

// in renderer
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc
  .send('notify', { "name": "Jeff" }')
  .then(() => console.log('You wrote the settings!'))
  .catch((e) => console.error(e));

如果传入其 WebContents 对象,您还可以将数据从主进程发送到渲染器。

// in main process
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc
  .send('getRendererData', webContentsForRenderer)
  .then((rendererData) => console.log(rendererData))
  .catch((e) => console.error(e));

// in renderer
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc.on('getRendererData', () => {
  return getSomeSuperAwesomeRendererData();
});

Removing Listeners

您可以使用 off() 方法删除侦听器。

const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc.on('someRoute', () => {
  return something();
});

promiseIpc.off('someRoute'); // never mind

@2o3t/electron-ipc-promise

Installation

yarn add @2o3t/electron-ipc-promise

Usage

// in main process
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc.on('notify', (newSettings) => {
    console.log(newSettings);
    return 'test';
});

// in renderer
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc
  .send('notify', { "name": "Jeff" }')
  .then(() => console.log('You wrote the settings!'))
  .catch((e) => console.error(e));

You can also send data from the main process to a renderer, if you pass in its WebContents object.

// in main process
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc
  .send('getRendererData', webContentsForRenderer)
  .then((rendererData) => console.log(rendererData))
  .catch((e) => console.error(e));

// in renderer
const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc.on('getRendererData', () => {
  return getSomeSuperAwesomeRendererData();
});

Removing Listeners

You can remove a listener with the off() method.

const promiseIpc = require('@2o3t/electron-ipc-promise');

promiseIpc.on('someRoute', () => {
  return something();
});

promiseIpc.off('someRoute'); // never mind
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文