Electron初步

Electron文档

mkdir myapp
cd myapp
npm init

生成的package.json如下,然后添加一行"start": "electron ."

{
  "name": "journal",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron ."
  },
  "keywords": [
    "journal"
  ],
  "author": "Octo",
  "license": "ISC",
}

安装electron,也可全局安装,不过不好对不同app做版本控制

npm install electron --save-dev
# 全局安装
npm install electron -g

package.json文件更新如下

"devDependencies": {
    "electron": "^3.0.1"
  }

当前目录下新建index.js文件 https://github.com/electron/e...

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

let mainWindow;

function createWindow() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600
    });
    mainWindow.loadFile("../src/index.html");
    mainWindow.on("closed", function() {
        mainWindow = null;
    });
};

app.on("ready", createWindow);
app.on("window-all-closed", function() {
    if (process.platform !== "darwin") {
        app.quit();
    }
});
app.on("activate", function() {
    if (mainWindow === null) {
        createWindow();
    }
});

运行npm start即可查看效果。

Electron API 演示应用程序

相关推荐