简体   繁体   English

尝试使用 JavaScript 打开新窗口时出错

[英]Error while trying to open a new window with JavaScript

I'm creating an electron with JavaScript but when I try to create a new window it gives me the error at the bottom of the page (click the link).我正在使用 JavaScript 创建一个电子,但是当我尝试创建一个新窗口时,它在页面底部出现错误(单击链接)。 I don't know what to do cause I cannot find the error.我不知道该怎么做,因为我找不到错误。

function createAddWindow() {
  addWindow = new BrowserWindow({
    width: 200,
    height: 200,
    title: 'Administrator Panel'
  });
  addWindow.loadURL(url.format({
    pathname: path.join(__dirname, "addWindow.html"),
    protocol: 'file:',
    slashes: true
  }));
}

const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
  Menu.setApplicationMenu(mainMenu);
});

const mainMenuTemplate = [
  {
    label: 'File',
    submenu:  [
      {
        label: 'Administrator Panel',
        click() {
          createAddWindow();
        }
      },
      {
        label: 'Exit',
        accelerator: process.platform == 'darwin' ? 'Command + Q' : 'Ctrl + Q',
        click() {
          app.quit();
        }
      }
    ]
  }
];

错误

https://hastebin.com/amepavehux.js https://hastebin.com/amepavehux.js

Can someone help with out with that?有人可以帮忙解决这个问题吗?

Ok, your problem is your function is out of the global scope.好的,您的问题是您的功能超出了全局范围。 You can: A.) Create a global function or B.) Remove the unnecessary function and do something such as this:您可以:A.) 创建一个全局函数或 B.) 删除不必要的函数并执行如下操作:

 const mainMenu = Menu.buildFromTemplate(mainMenuTemplate); Menu.setApplicationMenu(mainMenu); }); const mainMenuTemplate = [ { label: 'File', submenu: [ { label: 'Administrator Panel', click() { addWindow = new BrowserWindow({ width: 200, height: 200, title: 'Administrator Panel' }); addWindow.loadURL(url.format({ pathname: path.join(__dirname, "addWindow.html"), protocol: 'file:', slashes: true })); } }, { label: 'Exit', accelerator: process.platform == 'darwin' ? 'Command + Q' : 'Ctrl + Q', click() { app.quit(); } } ] } ];

I simply removed the function and put its code inside of the click handler.我只是删除了该函数并将其代码放在click处理程序中。

If you are determined to use the function, read up on global variables here: https://electronjs.org/docs/api/remote如果您决定使用该函数,请在此处阅读全局变量: https : //electronjs.org/docs/api/remote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM