electron ipcRenderer EventEmitter不适用于电容器和电子/vue

cnjp1d6j  于 2022-12-08  发布在  Electron
关注(0)|答案(1)|浏览(181)

我正在开发一个同时运行electron和capacitor android的项目。当我运行electron应用程序时,我得到了这个错误。它与预加载脚本有关。这个脚本允许我在主线程和vue线程之间进行IPC。安装capacitor后,我的electron IPC都不工作。
这是错误:

Uncaught Error: Cannot read properties of undefined (reading 'length')
        at EventEmitter.<anonymous> (VM115 preloadx.js:49:57)
        at EventEmitter.emit (VM14 node:events:390:28)
        at Object.onMessage (VM113 renderer_init:69:746)

这是呈现器脚本。它位于公共文件夹中。节点将它放在“out”文件夹中,在执行过程中可以在该文件夹中找到它。

const {
        contextBridge,
        ipcRenderer, 
    } = require("electron");

     let validChannels = [
                "api",
                "ascii",
                // some channels here ...
                "py-neo",
            ];


    contextBridge.exposeInMainWorld(
        "api", {
        send: (channel, data) => {
            // whitelist channels
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
                    if (validChannels.includes(channel)) {
                // Deliberately strip event as it includes `sender` 
                ipcRenderer.on(channel, (event, ...args) => func(...args)); // <-- error on this line!!
            }
        },
        sendSync: (channel, data) => {
            // whitelist channels
            if (validChannels.includes(channel)) {
                return ipcRenderer.sendSync(channel, data);
            }
        },

    }
    );

下面是主线程js文件中的一些代码。

function createWindow() {
      console.log("createWindow", __dirname);
      mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
          //sandbox: true,
          contextIsolation: true, 
          enableRemoteModule: true, 
          nodeIntegration: true,  
          nodeIntegrationInWorker: true, 
          preload: path.join(__dirname, "out", "preload.js"), // use a preload script
        },
      });

我使用的是electron 18.1.0和node 16.15.1。我希望在桌面上启动时IPC可以正常工作。在Android Studio中启动时IPC不需要工作。以下是我的package.json文件中的一段。

"scripts": {
"start": "vue-cli-service build && electron --disable-gpu --disable-software-rasterizer .  ",
 "dist": "electron-builder",
 "build": "vue-cli-service build",
 "android": "npx cap sync android"
  },
bq9c1y66

bq9c1y661#

我执行了类似下面的操作来删 debugging 误消息。

contextBridge.exposeInMainWorld(
      "api", {
      send: (channel, data) => {
          // whitelist channels
          if (validChannels.includes(channel)) {
              ipcRenderer.send(channel, data);
          }
      },
      receive: (channel, func) => {
          if (validChannels.includes(channel)) {
              // Deliberately strip event as it includes `sender` 
              ipcRenderer.on(channel, (event, ...args) => {
                console.log(channel , ...args);
                try {
                  func(...args);

                }
                catch {
                  console.log(...args);
                }
                //func(...args)
              });
          }
      },
      sendSync: (channel, data) => {
          // whitelist channels
          if (validChannels.includes(channel)) {
              return ipcRenderer.sendSync(channel, data);
          }
      },

  }
  );

相关问题