我需要为我的 render.js 文件导入菜单。但是要么 Remote 未定义,要么我得到一些错误。
使用以下代码我得到这个错误:
未捕获的类型错误:无法读取未定义的属性(读取“成员”)
我还使用 .enable(webContents) 方法尝试了 Electron 文档中的一些内容,但我也无法使其正常工作。
这样做的方法是什么?
Index.js:
const { app, BrowserWindow } = require('electron');
const path = require('path');
// 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,
contextIsolation: false,
}
});
render.js:
const { desktopCapturer } = require('electron');
const { Menu } = require('@electron/remote')
console.log(Menu);
最佳答案
如 npm @electron/remote 文档中所述
@electron/remote/main must be initialized in the main process before it can be used from the renderer:
// in the main process:
require('@electron/remote/main').initialize()
https://stackoverflow.com/questions/69697396/