Electron 主进程和渲染进程传值及窗口间传值

1 渲染进程调用主进程得方法

下面是渲染进程得代码:

复制代码
let { ipcRenderer} = require( 'electron' );
复制代码
ipcRenderer.send( 'xxx' );  //渲染进程中调用

下面是主进程得代码:

复制代码
var { ipcMain } = require( 'electron' );

ipcMain.on("xxx",function () { } )


2 渲染进程与渲染进程之间的传值

使用remote.BrowserWindow在某个渲染进程中打开一个新窗口,并向新窗口中传值

复制代码
xxxrWindow = new remote.BrowserWindow( {
			webPreferences: {
				nodeIntegration: true
			},
			show: true
		} );

xxxrWindow.webContents.on( 'did-finish-load', () => {
			xxxrWindow.webContents.send( "data", JSON.stringify(passInfo) );
			remote.getCurrentWindow().close();//关闭当前窗口
		} );

然后在新窗口中html开始Script标签内加入接收监听代码

复制代码
	//数据监听
		const ipc = require('electron').ipcRenderer;
		ipc.on('data', (e,arg) => {
			console.log("+++++++++++++++uu+++++++++++++++++++++++++");
			console.log(arg)
		});

注意 注意 注意消息发送后,才能关闭当前窗口,否则 打开的新窗口是接收不到消息的

也就是remote.getCurrentWindow().close(); 这行代码要写在上面的回调里面,保证代码发送完毕后,才关掉当前窗口,如果这行代码写在外面,窗口会先于代码发送关掉,这样代码发送就没执行


FR:hunkXu

相关推荐
yuanyxh2 小时前
Mac 软件推荐
前端·javascript·程序员
万少2 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
某人辛木2 小时前
Web自动化测试
前端·python·pycharm·pytest
Kagol2 小时前
Superpowers GSD gstack AgentSkills深度测评
前端·人工智能
excel3 小时前
JavaScript 字符串与模板字面量:从表象到本质理解
前端
京东云开发者4 小时前
当AI成为导演-如何用AI创作动漫短剧
前端
李白的天不白4 小时前
使用 SmartAdmin 进行前后端开发
java·前端
乘风gg4 小时前
🤡PUA AI Coding 工具 的 10 条终极语录
前端·ai编程·claude
学Linux的语莫4 小时前
Vue 3 入门教程
前端·javascript·vue.js
怕浪猫5 小时前
第一章、Chrome DevTools Protocol (CDP) 详解
前端·javascript·chrome