electron 内主进程与渲染进程的通信

mac2024-05-25  28

目录

一、资料分享

二、代码按例

三、注意事项


开发electron项目的时候,用到了electron的主进程与渲染进程直接的通信,

如果electron的主进程和渲染进程不太了解的话,建议看一下文档:

一、资料分享

1、刚开始学electron,建议先看第一个文档,对electron有个大概的认识

    地址:https://simulatedgreg.gitbooks.io/electron-vue/content/cn/main-process.html

2、electron的官网地址;https://electronjs.org/

3、W3Cschool的electron的中文文档地址: https://www.w3cschool.cn/electronmanual/

4、ElectronV5.0.0官网文档地址: https://www.bookstack.cn/read/electron-v5/1.md

5、中文文档地址:https://wizardforcel.gitbooks.io/electron-doc/content/faq/electron-faq.html

二、代码按例

// 在主进程中 const {ipcMain} = require('electron') ipcMain.on('asynchronous-message', (event, arg) => { //主进程接收渲染进程的请求事件 console.log(arg) // prints "ping" //获取参数 ​ ​..... //处理事件的过程 event.sender.send('asynchronous-reply', 'pong') //将事件处理结果在以另一个响应返给渲染进程 }) //在渲染器进程 (网页) 中 const {ipcRenderer} = require('electron') ipcRenderer.on('asynchronous-reply', (event, arg) => { //渲染进程接收主进程响应回来的处理结果 ​console.log(arg) // prints "pong" }) ipcRenderer.send('asynchronous-message', 'ping')//向主进程请求事件,携带参数 

 

三、注意事项

因为我是使用的electron-vue搭建的项目,渲染进程写在created里面

created () {     const ipc = require('electron').ipcRenderer;      ipc.on('exitRoom', function(event, message) {       if (message === 'exitRoom') {         this.exitRoom(); // 监听到主进程的信息后,处理自己的逻辑即可       }     });

 

最新回复(0)