【发布时间】:2019-10-07 00:20:32
【问题描述】:
我正在编写一个应该加载初始屏幕的电子应用程序,然后打开一个新窗口。之后,启动画面应关闭。
但是我无法做到这一点。在我的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)
有人知道如何防止新创建的窗口关闭吗?
【问题讨论】:
-
为什么要在另一个窗口中加载启动画面。你不喜欢那个代码吗?
-
你应该在同一个进程中加载'splash'和'main'窗口,然后根据你需要的事件显示/隐藏,我会用我的代码示例发布答案。
标签: javascript node.js electron