Electron.js小例子
进入 hello
然后npm init 初始化
得到package.json
{ "name": "hello", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>my_electron</title> </head> <body> <h1>你好,世界!</h1> </body> </html>
index.js
// 引入electron const {app, BrowserWindow} = require('electron'); let win; function createWindow() { // 创建浏览器窗口 win = new BrowserWindow({ width: 800, height: 400, webPreferences: { nodeIntegration: true, }, }); // 加载index.html文件 win.loadFile('index.html'); // 自动打开开发者工具 win.webContents.openDevTools(); // 当 window 被关闭,这个事件会被触发 win.on('closed', () => { win = null; }); } // Electron 会在初始化后并准备,创建浏览器窗口时,调用这个函数 app.on('ready', createWindow); // 当全部窗口关闭时退出 app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (win === null) { createWindow(); } });
输入 electron hello\