生活随笔
收集整理的這篇文章主要介紹了
electron 主进程与渲染进程 渲染进程与渲染进程 之间的通信
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
主進(jìn)程與渲染進(jìn)程之間的通信
這是渲染進(jìn)程
// 渲染進(jìn)程執(zhí)行主進(jìn)程里面的方法,主進(jìn)程給渲染進(jìn)程反饋處理結(jié)果 。
var sendreplayDom=document.querySelector('#sendreplay');
sendreplayDom.onclick=function(){// alert('1213')//渲染進(jìn)程給主進(jìn)程廣播數(shù)據(jù)ipcRenderer.send('sendreplay','this is renderer aaa');
}
//接收主進(jìn)程廣播的事件
ipcRenderer.on('replay',function(event,data){console.log(data);
})
這是主進(jìn)程
//接收廣播 并且返回處理結(jié)果
ipcMain.on('sendreplay',function(event,data){console.log(data);// console.log(event);//主進(jìn)程給渲染進(jìn)程廣播數(shù)據(jù)event.sender.send('replay','ok haha');
})
渲染進(jìn)程與渲染進(jìn)程之間的通信
a.渲染進(jìn)程 openWindows.js
btn.onclick=function(){// alert('點擊了');var aid='12345678';ipcRenderer.send('openWindow',aid);
}
//接收news 傳過來的數(shù)據(jù)
ipcRenderer.on('toIndex',function(event,data){console.log(data)
})
b.主進(jìn)程 ipcMain.js
ipcMain.on('openWindow',function(event,aid){//1.獲取當(dāng)前窗口的對象 var winId=BrowserWindow.getFocusedWindow().id;//調(diào)用 BrowserWindow打開新窗口win=new BrowserWindow({width:400,height:300,webPreferences:{nodeIntegration: true}// frame:false,// fullscreen:true})win.loadURL(path.join('file:',__dirname,'../news.html'));//開啟新窗口的調(diào)試模式win.webContents.openDevTools();//通過win.webContents.send把當(dāng)前數(shù)據(jù)廣播給 news進(jìn)程win.webContents.on('did-finish-load',function(){win.webContents.send('toNews',aid,winId);})win.on('closed',()=>{win=null;})
})
c. 渲染進(jìn)程 new.js
ipcRenderer.on('toNews',function(event,aid,winId){// winId 第一個窗口的id//獲取 對應(yīng)id的窗口console.log(aid);var firstWin=BrowserWindow.fromId(winId);firstWin.webContents.send('toIndex','this is news');
})
總結(jié)
以上是生活随笔為你收集整理的electron 主进程与渲染进程 渲染进程与渲染进程 之间的通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。