Javascript Electron Menu() 不是构造函数

标签 javascript node.js electron

我正在使用 custom-electron-titlebar模块以创建自定义的标题栏。我正在尝试更改菜单中的项目,如文档中所示:
enter image description here
但是,当我尝试模仿上面显示的代码时,它会在运行应用程序时引发错误:
错误 :

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)
我该如何解决这个问题?我想要自定义菜单栏而不是默认菜单栏!
非常感谢您的帮助!

最佳答案

您正在尝试导入 MenuMenuItem来自模块custom-electron-titlebar ,但 package documentation说应该使用 remote.Menuremote.MenuItem .
您收到此确切错误消息的原因是在您的代码中,Menuundefined ,因为包没有提供它。因此,尝试将其用作构造函数会失败。
使用 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/

相关文章:

electron - 如何从 Electron 中的 WebContents 获取 BrowserWindow?

javascript - 通过 POST 的数组(Ajax)

javascript - 使用 c3.js 创建自定义圆环图或饼图

node.js - 如果我需要同步性以便用户有机会在提供输入之前响应输出怎么办?

node.js - 全栈 Angular 和meanjs之间的区别

node.js - 即使已建立连接,数据处理程序也不会触发

node.js - Electron handle 正常崩溃

javascript - 如何在长 html 文件中添加空格?

javascript - 我怎样才能发现不再调用 MediaRecorder.ondataavailble 的问题?

reactjs - 如何允许在 Windows 机器中从同一源代码安装两个不同的可执行文件