我正在使用 custom-electron-titlebar
模块以创建自定义的标题栏。我正在尝试更改菜单中的项目,如文档中所示:
但是,当我尝试模仿上面显示的代码时,它会在运行应用程序时引发错误:
错误 :
Uncaught TypeError: Menu is not a constructor
这是我的代码:const { customTitlebar, Menu, Titlebar, Color, MenuItem } = require('custom-electron-titlebar');
titleBar = new Titlebar({
backgroundColor: Color.fromHex('#282a36'),
minimizable: false,
itemBackgroundColor: Color.fromHex('#44475a'),
})
const menu = new Menu();
menu.append(new MenuItem({
label: 'Item 1',
submenu: [
{
label: 'Subitem 1',
click: () => console.log('Click on subitem 1')
},
{
type: 'separator'
}
]
}));
console.log(menu)
titleBar.updateMenu(menu)
我该如何解决这个问题?我想要自定义菜单栏而不是默认菜单栏!非常感谢您的帮助!
最佳答案
您正在尝试导入 Menu
和 MenuItem
来自模块custom-electron-titlebar
,但 package documentation说应该使用 remote.Menu
和 remote.MenuItem
.
您收到此确切错误消息的原因是在您的代码中,Menu
是 undefined
,因为包没有提供它。因此,尝试将其用作构造函数会失败。
使用 remote
渲染器进程中的模块,您需要设置 enableRemoteModule
创建对应的 BrowserWindow
时.
const { remote } = require('electron')
const { Menu, MenuItem } = remote
如果您在主流程中处理菜单,则更容易:const { Menu, MenuItem } = require('electron');
关于Javascript Electron Menu() 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64639402/