Electron JS事件多次触发

2023-01-29前端开发问题
10

本文介绍了Electron JS事件多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 electronJS 中,我创建了一个自定义应用程序菜单,我在其中将事件从主进程发送到渲染器进程,但现在发生的事情是我正在监听该事件多次运行的位置.因此,如果有人可以帮助我找到并解决错误.谢谢.这是我的代码:

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

现在这个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')
})

},

推荐答案

尝试在你的关闭窗口函数中使用 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');
})

这篇关于Electron JS事件多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

仅使用 css 菜单正确显示第三层子菜单
Displaying third tier submenus properly with css only menu(仅使用 css 菜单正确显示第三层子菜单)...
2023-11-30 前端开发问题
4

IE6 CSS 悬停菜单问题
IE6 CSS Hover issues with menu(IE6 CSS 悬停菜单问题)...
2023-11-30 前端开发问题
2

css 菜单悬停“挂起"在铬 &苹果浏览器
css menu hover quot;hangsquot; in chrome amp; safari(css 菜单悬停“挂起在铬 amp;苹果浏览器)...
2023-11-30 前端开发问题
2

鼠标悬停或悬停 vue.js
Mouseover or hover vue.js(鼠标悬停或悬停 vue.js)...
2023-11-29 前端开发问题
28

是否可以使用 vue-chartjs 打印图表?
Is it possible to print a chart with vue-chartjs?(是否可以使用 vue-chartjs 打印图表?)...
2023-11-02 前端开发问题
4

(Vue, ChartJS) 从子组件画布上下文为图表创建渐变背景
(Vue, ChartJS) Create gradient background for chart from child component canvas context((Vue, ChartJS) 从子组件画布上下文为图表创建渐变背景)...
2023-11-02 前端开发问题
6