- 主進程與渲染進程之間的通信
這是渲染進程
// 渲染進程執行主進程里面的方法,主進程給渲染進程反饋處理結果 。
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');
})