从菜单打开新窗口

时间:2019-10-06 20:52:45

标签: javascript electron

当尝试从菜单打开窗口时,出现如下错误:“试图在已关闭或释放的渲染器窗口中调用函数”,但是调用其他窗口没有任何问题。

const ventana = document.getElementById("ventana");
const contrasena = document.getElementById("contra");
const remote = require("electron").remote;
const Menu = remote.Menu;
const BrowserWindow = remote.BrowserWindow;
const url = require("url");
const path = require("path");

let secundario
let recuperaVentana
let add

ventana.addEventListener("click", () => {
    var usuario = document.getElementById("user").value;
    var pass = document.getElementById("pass").value;
    if(usuario != "" & pass != ""){
        createBrowserWindow();
    } else {
        alert("No ha llenado los campos");
    }
});

contrasena.addEventListener("click", () => {
    recuperarBrowserWindow();
})

function createBrowserWindow() {
    secundario = new BrowserWindow({
        height: 600,
        width: 800
    });
    secundario.loadURL(url.format({
        pathname: path.join(__dirname, "./prefs.html"),
        protocol: "file",
        slashes: true
    }));

    //secundario.webContents.openDevTools();

    //menu
    const menuSec = Menu.buildFromTemplate(templateMenu);
    Menu.setApplicationMenu(menuSec);

    cerrar();

}

function recuperarBrowserWindow(){
    recuperaVentana = new BrowserWindow ({
        title: "Recuperar"
    })
    recuperaVentana.loadURL(url.format({
        pathname: path.join(__dirname, "./recuperar.html"),
        protocol: "file",
        slashes: true
    }))
    recuperaVentana.setMenu(null);
}

//cerrando ventana
function cerrar(){
    window.close();
}

//creando menus
const templateMenu = [
    {
        label: "Archivo",
        submenu: [
            {
                label: "Nuevo",
                accelerator: "Ctrl+N",
                click(){
                    addBrowserWindow();
                }
            },
            {
                label: "Abrir",
                accelerator: "Ctrl+O"
            },
            {
                label:"Guardar",
                accelerator:"Ctrl+G"
            },
            {
                type: "separator"
            },
            {
                label:"Salir"
            }
        ]
    },
    {
        label: "Ayuda"
    }

];

function addBrowserWindow() {
    add = new BrowserWindow({
        title: "Agregar"
    })
    add.loadURL(url.format({
        pathname: path.join(__dirname, "./agregar.html"),
        protocol: "file",
        slashes: true
    }))
}

尝试调用函数“ addBrowserWindow()”时,显示上述错误

1 个答案:

答案 0 :(得分:0)

这是相当简洁的代码版本,可在选择子菜单时创建第二个窗口:

const electron = require('electron')

const { app, BrowserWindow, Menu } = electron

let mainWindow
let secondWindow

app.on('ready', () => {
  console.log('Starting Node version: ' + process.version)
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: true,
    },
  })
  mainWindow.loadURL(`file://${__dirname}/main.html`)
  mainWindow.on('closed', () => app.quit())

  const mainMenu = Menu.buildFromTemplate(menuTemplate)
  Menu.setApplicationMenu(mainMenu)
})

function createSecondWindow() {
  secondWindow = new BrowserWindow({
    width: 300,
    height: 200,
    title: 'Add New Todo',
  })
  secondWindow.loadURL(`file://${__dirname}/add.html`)
}

const menuTemplate = [
  {
    label: 'File',
    submenu: [
      {
        label: 'New ToDo',
        click() {
          createSecondWindow()
        },
      },
      {
        label: 'Quit',
        accelerator: process.platform === 'darwin' ? 'Command+Q' : 'Ctrl+Q',
        click() {
          app.quit()
        },
      },
    ],
  },
]

if (process.platform === 'darwin') {
  menuTemplate.unshift({})
}

请注意,默认情况下, 当您关闭主窗口时,第二个窗口仍然存在-这是很少需要的怪异行为。解决方案是确保在关闭主窗口时调用app.exit()