简体   繁体   English

Electron 反应在 ipcMain 和 ipcRenderer 中双向发送数据

[英]Electron react sending data two-way in ipcMain and ipcRenderer

I am trying to create a communication between ipcMain and ipcRenderer in electron.我正在尝试在 electron 中创建ipcMainipcRenderer之间的通信。 I am using electron react boilerplate我正在使用electron 反应样板

So first step was adding this part to main:所以第一步是将这部分添加到main:

app
  .whenReady()
  .then(() => {
    ipcMain.handle('dialog:openFile', () => {
      console.log('test');
    });

second step was to add it in preload file:第二步是将其添加到preload文件中:

contextBridge.exposeInMainWorld('electronAPI', {
  openFile: () => ipcRenderer.invoke('dialog:openFile'),
});

and the third step is where I get stuck.第三步是我卡住的地方。 I am trying to add it in react component by:我正在尝试通过以下方式将其添加到反应组件中:

await window.electronAPI.openFile();

But I get Property 'electronAPI' does not exist on type 'Window & typeof globalThis'.但我得到Property 'electronAPI' does not exist on type 'Window & typeof globalThis'. error.错误。 What is the issue here?这里有什么问题?

Hello @Webby are you sure that the preload file is loaded in the browserWindow Object?您好@Webby,您确定在 browserWindow Object 中加载了预加载文件吗? Also that is necessary that you have the property contextIsolation on True此外,您必须在True上拥有属性 contextIsolation

something like this像这样的东西

const win = new BrowserWindow(
{
  fullscreen: false,
  webPreferences: {
    contextIsolation: true,
    preload: path.join(__dirname, '../path/to/your/preload.js'),
  },
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM