簡單建立一個electron專案
阿新 • • 發佈:2022-01-05
-
electron中文教程:https://www.w3cschool.cn/electronmanual/p9al1qkx.html
專案目錄:
安裝electron
npm install [email protected] --save-dev
package.json
{ "name": "electronTest", "version": "1.0.0", "description": "", "main": "main.js", // 專案入口 不指定預設為index.js "scripts": { "test": "echo \"Error: no test specified\" && exit 1","serve": "electron ." //啟動命令 }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "electron": "^4.2.12" } }
main.js
const { app, BrowserWindow } = require('electron') // 保持一個對於 window 物件的全域性引用,不然,當 JavaScript 被 GC, // window 會被自動地關閉 var mainWindow = null; // 當所有視窗被關閉了,退出。app.on('window-all-closed', function() { // 在 OS X 上,通常使用者在明確地按下 Cmd + Q 之前 // 應用會保持活動狀態 if (process.platform != 'darwin') { app.quit(); } }); // 當 Electron 完成了初始化並且準備建立瀏覽器視窗的時候 // 這個方法就被呼叫 app.on('ready', function() { // 建立瀏覽器視窗。 mainWindow = new BrowserWindow({width: 800, height: 600}); // 載入應用的 index.htmlmainWindow.loadURL('file://' + __dirname + '/index.html'); // 開啟開發工具 mainWindow.openDevTools(); // 當 window 被關閉,這個事件會被髮出 mainWindow.on('closed', function() { // 取消引用 window 物件,如果你的應用支援多視窗的話, // 通常會把多個 window 物件存放在一個數組裡面, // 但這次不是。 mainWindow = null; }); });
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>Hello World!</h1> We are using io.js <script>document.write(process.version)</script> and Electron <script>document.write(process.versions['electron'])</script>. </body> </html>
啟動: npm run serve
-