問題描述
在 electronJS 中,我創(chuàng)建了一個自定義應(yīng)用程序菜單,我在其中將事件從主進程發(fā)送到渲染器進程,但現(xiàn)在發(fā)生的事情是我正在監(jiān)聽該事件多次運行的位置.因此,如果有人可以幫助我找到并解決錯誤.謝謝.這是我的代碼:
In electronJS, I have created a custom application menu in which I'm sending the event from main process to renderer process, but now what happening is where I'm listening this event is running multiple times. So, if anyone could help me to find and resolve the error. Thanks. Here's my code:
label: test,
click: function (item, focusedWindow, event) {
mainWindow.webContents.send('test')
}
ipcRenderer.on('test', (event, action) => {
console.log('called')
})
現(xiàn)在這個console.log被打印了多次.
Now this console.log is printed multiple times.
原代碼:
{
label: constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.LABEL,
accelerator: constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.ACCELERATOR,
click: function (item, focusedWindow, event) {
contents.send(constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.EVENT)
}
}
created: function () {
ipcRenderer.on(constants.APPLICATION_MENU.ARTICLE.MENU.KEYWORD.EVENT, () => {
console.log('clicked')
})
},
推薦答案
嘗試在你的關(guān)閉窗口函數(shù)中使用 ipc.removeAllListeners('your_name_channel')
:
Try using ipc.removeAllListeners('your_name_channel')
in your closed window function:
Your_Window.on('closed',()=>{
ipc.removeAllListeners('your_name_channel');
})
這篇關(guān)于Electron JS事件多次觸發(fā)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!