javascript - 渲染过程中IPCRenderer的 Electron 错误

标签 javascript node.js electron

我正在学习Electron和更多的 Node ,但是每次与IPC Renderer交互时,我总是遇到错误。

render.js:6 Uncaught ReferenceError: Cannot access 'ipc' before initialization
at updateRP (render.js:6)
at HTMLButtonElement.onclick (index.html:11)
据我从各种论坛上了解到的,当我将nodeIntergation添加到我的主流程中时,该问题应该已经得到解决。我真的很困惑,对此的任何帮助将不胜感激。
CODE:
的HTML
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <link rel="stylesheet" href="index.css">
  </head>
  <body>
    <h1>💖 Hello World!</h1>
    <p>Welcome to your Electron application.</p>
    <button onclick="updateRP()">Update RP</button>
    <script type="text/javascript" src="render.js"></script>
  </body>
</html>
主要的
const { app, BrowserWindow } = require('electron');
const { ipcMain } = require('electron');
const ipc = require('electron').ipcMain;
const path = require('path');
const client = require('discord-rich-presence')('745419354375454901');
 
client.updatePresence({
  state: 'slithering',
  details: '🐍',
  startTimestamp: Date.now(),
  endTimestamp: Date.now() + 1337,
  largeImageKey: 'snek_large',
  smallImageKey: 'snek_small',
  instance: true,
});

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});


ipcMain.on("UpdateRP", stateForRP =>{
  client.updatePresence({
  state: stateForRP,
  details: 'test',
  startTimestamp: Date.now(),
  endTimestamp: Date.now() + 1337,
  largeImageKey: 'logo',
  smallImageKey: 'profilepic',
  instance: true,
});
});
使成为
const {ipcRenderer} = require('electron')
const ipc = electron.ipcRenderer;

function updateRP(){
    var stateForRP = "Test";
    ipc.send("UpdateRP", stateForRP);
}


最佳答案

至少可以说,在提供的代码中,由require ('electron')返回的对象的destructuring assignment的处理方式很奇怪。
渲染器中:

const {ipcRenderer} = require('electron')
const ipc = electron.ipcRenderer; // The variable *electron* has never been defined!
应该:
const electron = require('electron');
const ipc = electron.ipcRenderer;
或者:
const { ipcRenderer } = require('electron');
const ipc = ipcRenderer;
或者:
const { ipcRenderer: ipc } = require('electron');
同样,在中:
const { app, BrowserWindow } = require('electron');
const { ipcMain } = require('electron');
const ipc = require('electron').ipcMain;
可以重写为:
const { app, BrowserWindow, ipcMain } = require('electron');
const ipc = ipcMain;
或更简而言之:
const { app, BrowserWindow, ipcMain: ipc } = require('electron');
[更新]
我刚刚在主流程代码中注意到了另一个潜在问题:mainWindow变量必须是全局变量,这样它的值才不会被垃圾收集...
看到这个post
代替:
const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
};
使用:
let mainWindow;

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
};

关于javascript - 渲染过程中IPCRenderer的 Electron 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63825332/

相关文章:

javascript - 使用覆盖 hack 隐藏 iOS 7 中的工具栏

javascript - 如何将 JST 与 underscore.js 一起使用?

javascript - .findOne 在 Iron Router Route 的数据属性中工作,但 .find 不工作?

javascript - 使用大图像(文件大小)但不影响加载时间?

node.js - GetCursorPos Node FFI - 如何通过 ref 获取指针返回

angular - Uncaught TypeError : r. existsSync 不是 Angular 6 和 Electron 桌面应用程序中的函数

javascript - 尽管使用setState(),但是状态更改后,React组件不会重新呈现

javascript - 在 Azure Functions 中使用变量出绑定(bind)

node.js - socket.io websocket 连接无效(最新的 chrome)

electron - 如何使 CTRL-SHIFT-S 绑定(bind)到 Save All in Atom?