- 主进程与渲染进程之间的通信
这是渲染进程
// 渲染进程执行主进程里面的方法,主进程给渲染进程反馈处理结果 。
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');
})
- 渲染进程与渲染进程之间的通信
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');
})