Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

vue.js - Electron JS event is firing multiple times

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')
    })

Now this console.log is printed multiple times.

original code:

{
  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')
})

},

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So after a lot of searching I found the answer.If you are switching the routes and registered some channels on one component and some on other, So you can remove the listeners for the particular channels in a lifecycle method(destroyed) when the component is unmounted. My issue was I was switching between routes and every time created was running in which I registered ipc renderer to listen to those channels. So i removed the listeners to the channels in destroyed lifecycle hook.

It can be done by:

ipcrenderer.removeAllListeners([channel])

Here's is the link for docs: Electron


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...