Electron和NW.js入门笔记

安装cnpm

因为npm在国内的下载比较慢,所以先命令行安装cnpm:

npm install -g cnpm --registry=https://registry.npm.taobao.org

安装NW.js

cnpm install -g nw

构建 Hello World 应用

demo1

package.json:

{
    "name": "demo1",
    "main": "index.html",
    "version": "1.0.0"
}

index.html:

<html>
    <title>Hello World</title>
    <script>
        function sayHello() {
            alert('Hello World');
        }
    </script>
    <body>
        <button onclick="sayHello()">Say Hello</button>
    </body>
</html>

运行:

nw

安装 Electron

cnpm install -g electron

使用 Electron 开发 Hello World 应用

demo2

index.html:

<html>
    <title>Hello World</title>
    <script>
        function sayHello() {
            alert('Hello World');
        }
    </script>
    <body>
        <button onclick="sayHello()">Say Hello</button>
    </body>
</html>

package.json:

{
    "name": "hello-world",
    "version": "1.0.0",
    "main": "main.js"
}

main.js:

'use strict';

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

let mainWindow = null;

app.on('ready', () => {
    mainWindow = new BrowserWindow();
    mainWindow.loadURL(`file://${__dirname}/index.html`);
    mainWindow.on('closed', () => { mainWindow = null; })
});

运行:

electron .
posted @ 2020-06-08 13:54  zifeiy  阅读(528)  评论(0编辑  收藏  举报