电子 - 关闭初始窗口但让孩子保持开放

时间:2018-01-12 10:19:31

标签: javascript node.js electron

我正在编写一个应该加载启动画面的电子应用程序,然后打开一个新窗口。然后应关闭启动画面。

但是我无法做到这一点。在我的index.js启动脚本中,我有以下代码:

const { app, BrowserWindow } = require("electron");

app.on("ready", () => {
    let win = new BrowserWindow({ /*...*/ });
    win.loadURL(`file://${__dirname}/splash/splash.html`);
    win.on("ready-to-show", () => { win.show(); });
    win.on("closed",        () => { app.quit(); });
});

splash.html中,我使用

加载splash.js
<script>require("./splash");</script>

在splash.js中,我尝试了以下代码:

const remote = require("electron").remote;

let tWin = remote.getCurrentWindow();

let next = function(){ 
    let win = new remote.BrowserWindow({ /*...*/ });
    win.loadURL(`file://${__dirname}/main/main.html`);
    win.on("ready-to-show", () => { win.show(); });
    win.on("closed",        () => { app.quit(); });

    tWin.close();

    // I could just use win.hide(); here instead 
    // of tWin.close(); but that can't really be the right way.
};

超时后调用函数next()。问题是,如果调用,主窗口会显示一秒钟,但两个,slpash和main立即关闭。

我试图通过评论来解决这个问题

win.on("closed", () => { app.quit(); });

在我的index.js中。但这导致了以下错误:

  

尝试在已关闭或释放的渲染器窗口中调用函数。

Uncaught Exception:
Error: Attempting to call a function in a renderer window that has been closed or released. Function provided here: splash.js:38:9.
    at BrowserWindow.callIntoRenderer (/usr/lib/node_modules/electron-prebuilt/dist/resources/electron.asar/browser/rpc-server.js:199:19)
    at emitOne (events.js:96:13)
    at BrowserWindow.emit (events.js:188:7)

有没有人知道如何防止新创建的窗口关闭?

1 个答案:

答案 0 :(得分:1)

我通常使用不同的方法。以下是使用方法:

  1. 为主窗口和启动窗口创建全局var引用,否则将由垃圾收集器自行关闭。
  2. 加载&#39; 启动 &#39; browserwindow
  3. on&#39; show &#39; event我调用一个函数加载&#39; main &#39;窗口
  4. 窗口&#39; dom-ready &#39;,我关闭&#39; splash < /强>&#39;并显示&#39; 主要&#39;
  5. 以下是我的 main.js 电子代码的一个示例,请随时询问:

       'use strict';
    
        //generic modules
        const { app, BrowserWindow, Menu } = require('electron');
        const path = require('path')
        const url = require('url')
    
        const config = require('./config'); //                              => 1: archivo de configuracion
        const fileToLoad = config.files.current ? config.files.current : config.files.raw;
        const jsonData = require(fileToLoad); //                            => 2: archivo de datos (json) 
        const pug = require('electron-pug')({ pretty: true }, jsonData); // => 3: pasamos datos ya tratados a plantillas pug/jade 
    
    
        // Keep a global reference of the window object, if you don't, the window will
        // be closed automatically when the JavaScript object is garbage collected.
        let win, loading
        app.mainWindow = win;
    
        function initApp() {
            showLoading(initPresentation)
        }
    
        function showLoading(callback) {
            loading = new BrowserWindow({ show: false, frame: false })
            loading.once('show', callback);
            loading.loadURL(url.format({
                pathname: path.join(__dirname, '/src/pages/loading.html'),
                protocol: 'file:',
                slashes: true
            }))
    
            loading.show();
        }
    
        function initPresentation() {
            win = new BrowserWindow({
                width: 1280,
                height: 920,
                show: false,
                webPreferences: {
                    experimentalFeatures: true
                }
            })
            win.webContents.once('dom-ready', () => {
                    console.log("main loaded!!")
                    win.setMenu(null);
                    win.show();
                    loading.hide();
                    loading.close();
                })
                // Emitted when the window is closed.
            win.on('closed', () => {
                // Dereference the window object, usually you would store windows
                // in an array if your app supports multi windows, this is the time
                // when you should delete the corresponding element.
                win = null
            })
            win.loadURL(url.format({
                //pathname: path.join(__dirname, '/src/pages/home.pug'),
                pathname: path.join(__dirname, '/lab/pug/index.pug'),
                protocol: 'file:',
                slashes: true
            }))
    
            win.webContents.openDevTools() // Open the DevTools.
        }
    
        // This method will be called when Electron has finished
        // initialization and is ready to create browser windows.
        // Some APIs can only be used after this event occurs.
        app.on('ready', initApp)
    
        // Quit when all windows are closed.
        app.on('window-all-closed', () => {
            // On macOS it is common for applications and their menu bar
            // to stay active until the user quits explicitly with Cmd + Q
            if (process.platform !== 'darwin') {
                app.quit()
            }
        })
    
        app.on('activate', () => {
            // On macOS it's common to re-create a window in the app when the
            // dock icon is clicked and there are no other windows open.
            if (win === null) {
                initApp()
            }
        })
    
        // In this file you can include the rest of your app's specific main process
        // code. You can also put them in separate files and require them here.*/