1:渲染层事件中心
const ipcRenderer = require('electron').ipcRenderer;
const sendBridge = (msg = { active: '', data: {} }) => {
return new Promise((resolve, reject) => {
ipcRenderer.on(msg.active, (event, arg) => {
resolve(arg);
});
console.log('8>>>bridge.js', msg);
ipcRenderer.send(msg.active, msg);
});
};
export default sendBridge;
1.2 渲染层 事件中心注册
import Vue from 'vue';
import sendBridge from './events/bridge';
Vue.prototype.$sendBridge = sendBridge;
2:主进程 接收并处理
index.js 注册IPCHandler
import IPCHandler from './IPCHandler';
let context = {}; // 全局上下文
context.app = app;
context.isDev = process.env.NODE_ENV !== 'production';
context.ipcHandler = new IPCHandler(context);
IPCHandler.js
const { ipcMain } = require('electron');
class IPCHandler {
constructor (context) {
this.mContext = null;
this.mContext = context;
// 通讯录
ipcMain.on('onInsertAllContacts', this.onInsertAllContacts.bind(this));
}
async onInsertAllContacts (event, args, userInfo) {
console.log(38, event, args);
event.sender.send(args.active, { active: args.active, result });
}
}
export default IPCHandler;
3:渲染层 调用
this.$sendBridge({active: 'onInsertAllContacts', data: []}).then(res => {
});
总体思路:
页面组件 (active) =》 事件中心(ipcRenderer active) =》主进程接收中心(ipcMain active)=》【主进程业务处理】=》主进程接收中心(ipcRenderer callback)=》页面组件 (active,callback)
另外本人这里有套关于小白学习的大量资料,有兴趣的同学可以点击这里。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。