目錄
- 一、所需環境&打包前準備
- 1、安裝node.js
- 2、安裝electron
- 3、web專案
- 二、打包程序
- 1、打包配置
- 2、 安裝打包器
- 3、執行打包命令:
Electron是由GitHub開發,使用 JavaScript,HTML 和 CSS 構建跨平臺的桌面應用程式,可以幫我們把web網頁專案直接打包成桌面應用程式,
一、所需環境&打包前準備
1、安裝node.js
electron 依賴于node.js 需要安裝node.js:
安裝node(步驟,不是重點,略過)
2、安裝electron
dos下,全域安裝:
npm install -g electron
檢查是否安裝成功:
electron -v
3、web專案
二、打包程序
1、打包配置
在需要打包的web網頁專案根目錄下分別新建:main.js、package.json
package.json:在package.json中加入如下內容
{
"name" : "App", // 專案名稱
"version" : "0.1.0",
"main" : "main.js" // 根目錄下的main.js
}
main.js:在main.js中加入如下代碼
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
注意:在main.js 中的index.html 為你web專案的首頁,及入口檔案

2、 安裝打包器
打開 DOS視窗,cd 到你的專案目錄下,輸入下面命令,全域安裝electron打包器
npm install electron-packager -g
3、執行打包命令:
在根目錄下執行打包命令:
electron-packager . app --win --out App --arch=x64 --electron-version 1.4.14 --overwrite --ignore=node_modules

打包成功后在根目錄下會生成打包好的工程:

打開工程中的應用程式即可運行web專案:


轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/294492.html
標籤:其他
上一篇:JSON(一)與XML對比、MIME、JSON物件、JSON陣列
下一篇:JavaScript 基礎
