Javascript Electron Menu从菜单模板文件打开应用程序窗口

时间:2017-05-13 22:27:45

标签: javascript electron

在我的Electron App中,我可以在外部本地文件中创建一个Menu模板,并将其命名为menuTemplate.js

菜单有效,但我希望能够从中打开本地文件,例如about.html

我已经尝试了' window.open(' url here')' 但它并不了解窗口......

以下是模板:

module.exports = [
  {
    label: 'Electron',
    submenu: [
      {label: 'Item 1'},
      {label: 'Item 2'}
    ]
  },
  {
    label: 'Actions',
    submenu: [
      {label: 'Action 1'},
      {label: 'Action 2'},
      {label: 'Action 3'},
      {role: 'toggledevtools'},
      {label: 'ClickMe', click () { window.open('url here'); } }
    ]
  }
]

我已经尝试了 shell.openExternal ,但它确实有效但我无法从此处打开应用程序窗口。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

虽然将这样的模板分成单独的文件是个好主意,但您无法访问原始文件的范围。要解决此问题,您必须将主窗口(假设称为main.js)中的窗口带入menuTemplate.js

例如,您可以通过创建在执行时构建模板的方法来执行此操作。它可能看起来像这样:

menuTemplate.js

module.exports = function(window){
    return [
      {
        label: 'Electron',
        submenu: [
          {label: 'Item 1'},
          {label: 'Item 2'}
        ]
      },
      {
        label: 'Actions',
        submenu: [
          {label: 'Action 1'},
          {label: 'Action 2'},
          {label: 'Action 3'},
          {role: 'toggledevtools'},
          {label: 'ClickMe', click () { window.open('url here'); } }
        ]
      }
    ]
}

现在,在main.js中加载模板时,您不会执行类似

的操作

const template = require('menuTemplate')

但是类似

const template = require('menuTemplate')(window)

用"窗口"是窗口变量的名称。

答案 1 :(得分:0)

这对我有用:

    label: 'General',
    submenu: [
      {label: 'Unidades',          
      click () {   mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, './app/fixed_footer.html'),
        protocol: 'file:',
        slashes: true

      })); }