electron 主进程与渲染进程 渲染进程与渲染进程 之间的通信

  1. 主进程与渲染进程之间的通信
这是渲染进程
// 渲染进程执行主进程里面的方法,主进程给渲染进程反馈处理结果 。
var sendreplayDom=document.querySelector('#sendreplay');
sendreplayDom.onclick=function(){
    // alert('1213')
    //渲染进程给主进程广播数据
    ipcRenderer.send('sendreplay','this is  renderer aaa');
}
//接收主进程广播的事件
ipcRenderer.on('replay',function(event,data){
    console.log(data);
})
这是主进程
//接收广播 并且返回处理结果
ipcMain.on('sendreplay',function(event,data){
    console.log(data);
    // console.log(event);
    //主进程给渲染进程广播数据
    event.sender.send('replay','ok  haha');
})
  1. 渲染进程与渲染进程之间的通信
a.渲染进程 openWindows.js
btn.onclick=function(){
    // alert('点击了');
    var aid='12345678';
    ipcRenderer.send('openWindow',aid);
}
//接收news 传过来的数据
ipcRenderer.on('toIndex',function(event,data){
    console.log(data)
})
b.主进程 ipcMain.js
ipcMain.on('openWindow',function(event,aid){
       
    //1.获取当前窗口的对象  
    var winId=BrowserWindow.getFocusedWindow().id;
    //调用 BrowserWindow打开新窗口
    win=new BrowserWindow({
        width:400,
        height:300,
        webPreferences:{
            nodeIntegration: true
        }
        // frame:false,
        // fullscreen:true
    })
    win.loadURL(path.join('file:',__dirname,'../news.html'));
    //开启新窗口的调试模式
    win.webContents.openDevTools();
    //通过win.webContents.send把当前数据广播给 news进程
    win.webContents.on('did-finish-load',function(){
        win.webContents.send('toNews',aid,winId);
    })
    win.on('closed',()=>{
        win=null;
    })
})

c. 渲染进程 new.js

ipcRenderer.on('toNews',function(event,aid,winId){
    // winId 第一个窗口的id
    //获取 对应id的窗口
    console.log(aid);
    var firstWin=BrowserWindow.fromId(winId);
    firstWin.webContents.send('toIndex','this is news');
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值