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

相关推荐
前端 贾公子4 分钟前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
GISer_Jing1 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能
明远湖之鱼2 小时前
一种基于 Service Worker 的渐进式渲染方案的基本原理
前端
前端小端长2 小时前
Vue 中 keep-alive 组件的原理与实践详解
前端·vue.js·spring
FeelTouch Labs2 小时前
Nginx核心架构设计
运维·前端·nginx
雪球工程师团队3 小时前
别再“苦力”写后台,Spec Coding “跑” 起来
前端·ai编程
m0_471199633 小时前
【场景】前端怎么解决离线收银、数据同步异常等场景问题
前端·javascript
Curvatureflight3 小时前
前端性能优化实战:从3秒到300ms的加载速度提升
前端·人工智能·性能优化
用户99045017780093 小时前
ruoyi集成dmn规则引擎
前端