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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值