您现在的位置是:网站首页> 编程资料编程资料

AntDesignPro使用electron构建桌面应用示例详解_JavaScript_

2023-05-24 316人已围观

简介 AntDesignPro使用electron构建桌面应用示例详解_JavaScript_

注意事项声明

  • 所有 node 包必须使用 npm 安装不可使用 cnpm, 使用 cnpm 安装的 node 包会导致打包时间无限可能
  • 具体区别查看使用 npmcnpm 安装的包结构
  • 所有包的均可以安装在全局, 避免重复安装

主要分为两个部分

开发环境使用

安装 electron 包

npm install electron --save-dev 

package.js 添加入口文件

 "main": "main.js", 

创建 main.js 入口文件 内容如下

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 模块

global.electron = require('electron') 

在 package.json 文件中加入启动命令

 "scripts": { "electron-start": "electron .", }, 

试启动 electron 窗口内容加载成功则成功

npm run electron-start 

渲染进程如需和主进程通信查看官方文档

https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes

打包应用配置

config/config.js 文件添加

history: 'hash', //更改路由方式 publicPath: './', //使打包后的文件使用相对路径 

src/utils/request.js 此目录并非标准 不同版本下文件可能有所区别 重点在于给请求配置前缀

当项目打包成应用后使用的是 file:协议ant pro 的请求无法发出 需要使用完整的请求地址 目前方法为配置前缀

/** * 配置request请求时的默认参数 */ const request = extend({ errorHandler, // 默认错误处理 prefix: 'http://hotel-system.yc384.com/api', // 请求前缀 credentials: 'include', // 默认请求是否带上cookie }); 

package.json配置打包后的路径方式

"homepage": ".", 

使用 electron-builder 打包 exe 文件或者安装包,压缩包

  • 提示:
  • 提前安装在全局可以省略不同环境重复安装
  • 创建 app 目录是为了不将 node 包打包进去,减少应用大小
  • 如果当前目录下没有 node 包或者内容较少可直接在当前操作, 省略 app 目录相关操作

安装

npm install electron-builder 

package.json添加命令 (打包windows)

"electron-build": "electron-builder --win --x64" 

添加打包配置

"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是打包后的应用依赖)

"name": "hotel", "version": "2.3.1", "main": "main.js", 

执行打包命令

打包后文件会在 build 目录下

npm run electron-build 

使用 electron-packager 打包成 exe 文件

安装electron-package

npm install electron-packager --save-dev 

package.json下script添加命令(具体含义百度)

"electron-package": "electron-packager . hotelSystem --win32 --out app --arch=x64 --overwrite --ignore=node_modulesls --electron-version=6.0.5", 

执行命令

npm run electron-package 

提示

  • 打包环境可以和开发环境分开 这样可以减少不必要依赖 缩短打包时间
  • 将打包后的 distmain.js 文件放入一个新目录
  • 配置 package.json 文件打包参数 其他删除即可
"name": "hotel", "version": "2.3.1", "main": "main.js", "homepage": ".", "scripts": { "electron-start": "electron .", }

以上就是AntDesignPro使用electron构建桌面应用示例详解的详细内容,更多关于AntDesignPro electron构建桌面的资料请关注其它相关文章!

-六神源码网