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

Javascript电子菜单从菜单模板文件打开应用程序窗口,javascript,electron,Javascript,Electron,在我的Electron应用程序中,我可以在一个外部本地文件中创建一个菜单模板,并将其命名为menuTemplate.js 菜单可以找到,但我希望能够从中打开一个本地文件,例如about.html 我尝试了“window.open('url here')”,但它不理解window 以下是模板: module.exports = [ { label: 'Electron', submenu: [ {label: 'Item 1'}, {label: 'It

在我的Electron应用程序中,我可以在一个外部本地文件中创建一个菜单模板,并将其命名为menuTemplate.js

菜单可以找到,但我希望能够从中打开一个本地文件,例如about.html

我尝试了“window.open('url here')”,但它不理解window

以下是模板:

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,它可以工作,但我无法从这里打开应用程序窗口


如何才能做到这一点?

虽然将这样的模板分离到单独的文件中是一个好主意,但您无法访问原始文件的范围。要解决此问题,必须将窗口从main文件(假定名为
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”是窗口变量的名称。

将这样的模板分离到一个单独的文件中是一个好主意,但您无法访问原始文件的范围。要解决此问题,必须将窗口从main文件(假定名为
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”是窗口变量的名称。

这就是我的工作原理:

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

      })); }

这就是我的工作原理:

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

      })); }