Javascript 不同的浏览器窗口在electron js中应有不同的菜单选项

Javascript 不同的浏览器窗口在electron js中应有不同的菜单选项,javascript,node.js,electron,Javascript,Node.js,Electron,我需要2个浏览器窗口,一个是主窗口,另一个是子窗口,我想设置子窗口-菜单,菜单不应反映在主窗口上 app.on('ready', function() { // Create new window // this assign the mainwindow variable as a browserwindow with // default parameter value which will take the entire page mainWindow = ne

我需要2个浏览器窗口,一个是主窗口,另一个是子窗口,我想设置子窗口-菜单,菜单不应反映在主窗口上

app.on('ready', function() {
  // Create new window
  // this assign the mainwindow variable as a browserwindow with 
  //      default parameter value which will take the entire page
  mainWindow = new BrowserWindow({});
  childWindow = new BroserWindow({});
  // Load html in window
  // the below function will load the html into the window using the 
  //Command pathname
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'mainWindow.html'),
    protocol: 'file:',
    slashes: true
  }));
  childWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'childWindow.html'),
    protocol: 'file:',
    slashes: true
  }));
  / /
  Quit app when closed
  mainWindow.on('closed', function() {
    app.quit();
  });
  // Build menu from template
  const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
  // Insert menu
  Menu.setApplicationMenu(mainMenu);
});
//here i need to set the menu option only to the child window 
//not the main window

我不确定您的要求是什么,但我可以告诉您,您希望在主窗口上设置一个
menuBar
,在子窗口上设置一个不同的
menuBar

您可以这样做:

const mainWindowMenuBar  = Menu.buildFromTemplate(<Main window template>);
const childWindowMenuBar = Menu.buildFromTemplate(<Child window template>);

mainWindow.setMenu(mainWindowMenuBar);

childWindow.setMenu(childWindowMenuBar);
const mainWindowMenuBar=Menu.buildFromTemplate();
const childWindowMenuBar=Menu.buildFromTemplate();
设置菜单(主窗口菜单栏);
设置菜单(childWindowMenuBar);

您使用了
菜单。设置应用程序菜单(主菜单)如果没有明确定义菜单,则用于设置为每个窗口的顶部菜单。(请参阅文档)。您可以使用
MainWindow.setMenu(mainMenu)
将菜单仅限于MainWindow。此后,由于您的子窗口没有定义菜单,默认的顶部菜单将出现在那里