gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:25:18 25 4
gpt4 key购买 nike

我正在学习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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com