安装 electron 包
1 |
npm install electron --save-dev |
package.js 添加入口文件
1 |
"main": "main.js", |
创建 main.js 入口文件 内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
const {app, BrowserWindow, dialog} = 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 mainWindow; ? function createWindow () { ? // Create the browser window. mainWindow = new BrowserWindow({ width: 1300, height: 800, frame: true, autoHideMenuBar: true, fullscreenable: true, transparent: false, webPreferences: { javascript: true, plugins: true, nodeIntegration: true, // Nodejs webSecurity: false, preload: path.join(__dirname, './preload.js') } }); // mainWindow.webContents.openDevTools();//打开调试工具 ? //测试时使用mainWindow.loadURL(http://localhost:80000); //打包时加载本地文件 mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'dist/index.html'), protocol: 'file:', slashes: true })); ? // 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() }); |
preload.js 文件内添加, 将 electron 做全局导入 未做此操作无法在其他地方使用 electron 模块
1 |
global.electron = require('electron') |
在 package.json 文件中加入启动命令
1 2 3 |
"scripts": { "electron-start": "electron .", }, |
试启动 electron 窗口内容加载成功则成功
1 |
npm run electron-start |
渲染进程如需和主进程通信查看官方文档
1 |
https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes |
config/config.js 文件添加
1 2 |
history: 'hash', //更改路由方式 publicPath: './', //使打包后的文件使用相对路径 |
src/utils/request.js 此目录并非标准 不同版本下文件可能有所区别 重点在于给请求配置前缀
当项目打包成应用后使用的是 file:协议 ant pro 的请求无法发出 需要使用完整的请求地址 目前方法为配置前缀
1 2 3 4 5 6 7 8 |
/** * 配置request请求时的默认参数 */ const request = extend({ errorHandler, // 默认错误处理 prefix: 'http://hotel-system.yc384.com/api', // 请求前缀 credentials: 'include', // 默认请求是否带上cookie }); |
1 |
"homepage": ".", |
1 |
npm install electron-builder |
1 |
"electron-build": "electron-builder --win --x64" |
1 2 3 4 5 6 7 8 9 10 11 |
"build": { "appId": "com.xxx.app", "directories": { "output": "build" }, // 打包后存放目录 "mac": {// mac安装包dmg "target": ["dmg","zip"] }, "win": {// win安装包nsis "target": ["nsis","zip"] } |
创建app目录(builder默认打包app下内容,否则会打包当前所有内容)
将ant pro打包后的dist文件和main.js放入app目录
在app下创建package.json文件(外层package做打包使用,app下的package是打包后的应用依赖)
1 2 3 |
"name": "hotel", "version": "2.3.1", "main": "main.js", |
打包后文件会在 build 目录下
1 |
npm run electron-build |
安装electron-package
1 |
npm install electron-packager --save-dev |
package.json下script添加命令(具体含义百度)
1 |
"electron-package": "electron-packager . hotelSystem --win32 --out app --arch=x64 --overwrite --ignore=node_modulesls --electron-version=6.0.5", |
1 |
npm run electron-package |
1 2 3 4 5 6 7 |
"name": "hotel", "version": "2.3.1", "main": "main.js", "homepage": ".", "scripts": { "electron-start": "electron .", } |