使用 Electron 开发跨平台应用程序。

使用 Electron 开发跨平台应用程序。2018年12月12日

Ⅰ. 创建项目

npm init

然后跟随 Powershell 输入

执行完毕后安装 electron

npm install electron --save-dev

Ⅱ. 创建 main.js 并启动

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')

// 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 mainWindow

function createWindow () {
  // 设置窗口大小
  mainWindow = new BrowserWindow({width: 800, height: 600})

  // 启动本地路径中的 html 文件
  // mainWindow.loadFile("index.html");
  // 启动互联网 URL 地址
  mainWindow.loadURL("https://unus.me")
  // 打开开发工具
  // mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // 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.
    mainWindow = 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', function () {
  // 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', function () {
  // 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 (mainWindow === 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.

修改 package.json * 请删除注释的中文。

{
  "name": "unustranslate",  // 形目名称
  "version": "1.0.0", // 版本号
  "description": "Unus Translate Project", // 描述
  "main": "main.js", // 应用的主要设置
  "scripts": {
    "start": "electron ."  // npm start 开启 electron.
  },
  "author": "unus",  // 开发者
  "license": "MIT", // 许可证
  "devDependencies": {   // 在下方添加开发环境依赖模块
    "electron": "^3.0.11" 
  }
}

在文件夹路径中启动 Powershell,输入命令 npm start 就能启动你的应用程序。

Ⅲ. 关于打包

安装 Electron Packager

npm install electron-packager --save-dev

Electron Packager 地址:https://github.com/electron-userland/electron-packager

修改 package.json 代码

{
  "name": "unustranslate"
   ****
   ****
   添加这行 Build 设置代码 
   "win64": "electron-packager . --out=dist/windows64 --platform=win32 --arch=x64 --icon=assets/icon/favicon.ico --asar=true --overwrite",
   "linux64": "electron-packager . --out=dist/Linux64 --platform=linux --arch=x64 --icon=assets/icon/favicon.ico --asar=true --overwrite",
  "build": "./node_modules/.bin/electron-packager ./ --platform=win32 --arch=x64 --out=./build --asar --app-version=0.1.0"
},

执行 npm run build

如果打包 linux 输入 npm run linux64

在项目文件夹下的 build 中执行你打包的应用程序。

发表评论

电子邮件地址不会被公开。 必填项已用*标注