Electron Ipc 渲染发射事件到 Vuex

Electron Ipc render emit event to Vuex

大家好,我正在构建一个包含多个 windows、

的应用

我的应用是用 Vue + Electron 开发的

我试图完成的主要功能是当用户例如想要在电子打开弹出窗口中执行某些操作时,接下来我想将操作发送到 Vuex 存储以向用户显示消息

那么如何从 electron 调用 Vuex 动作?

代码示例:

Vue 组件:

import { remote, ipcRenderer } from 'electron';

ipcRenderer.send('openPopup', id);

电子代码:

import { ipcMain, app } from 'electron';


ipcMain.on('openPopup', (event, args) => {
    console.log('do some action');
    // now how can I call vuex action from here
});

我尝试调用它:

this.$store

但未定义

您可以简单地 return 在 ipcMain.on 事件中将所需数据返回给渲染器进程。

我还建议使用 ipcRenderer.invoke / ipcMain.handle (Electron Doc - ipcMain)

// Main process
ipcMain.handle('openPopup', async (event, ...args) => {
  const result = await somePromise(...args)
  return result
})

// Renderer process
async () => {
  const result = await ipcRenderer.invoke('open-popup', id)
  // this.$store.dispatch(....) etc etc
}