在实际的NW.js程序开发中,我们可能在程序启动时做一些加载前逻辑,比如更新等等,那如何实现等待这些逻辑完成后才开始加载index.html呢?

bootstrap.html

准备一个App启动页bootstrap.html,并在package.json中的main入口指向bootstrap.html,不再指向index.html

<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>启动中</title>
</head>
<body>
<p>启动中…</p>
</body>
</html>

bootstrap.js

package.json中的node-main入口指向bootstrap.js,这是在Node上下文中执行的启动逻辑,等待逻辑完成后我们就可以主动跳转到index.html。

// 启动逻辑
const main = nw.Window.get();
main.window.location.href=’/index.html’;
这样子我们就能实现“阻塞”index.html的加载了。