目录注意事项声明主要分为两个部分开发环境使用打包应用配置package.JSON配置打包后的路径方式使用 electron-builder 打包 exe 文件或者安装包,压缩包安装p
node
包必须使用 npm
安装不可使用 cnpm
, 使用 cnpm
安装的 node
包会导致打包时间无限可能npm
和 cnpm
安装的包结构安装 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
的请求无法发出 需要使用完整的请求地址 目前方法为配置前缀
const request = extend({
errorHandler, // 默认错误处理
prefix: 'http://hotel-system.yc384.com/api', // 请求前缀
credentials: 'include', // 默认请求是否带上cookie
});
"homepage": ".",
app
目录是为了不将 node
包打包进去,减少应用大小node
包或者内容较少可直接在当前操作, 省略 app
目录相关操作npm install electron-builder
"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-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
dist
和 main.js
文件放入一个新目录package.json
文件打包参数 其他删除即可"name": "hotel",
"version": "2.3.1",
"main": "main.js",
"homepage": ".",
"scripts": {
"electron-start": "electron .",
}
以上就是AntDesignPro使用electron构建桌面应用示例详解的详细内容,更多关于AntDesignPro electron构建桌面的资料请关注编程网其它相关文章!
--结束END--
本文标题: AntDesignPro使用electron构建桌面应用示例详解
本文链接: http://www.lsjlt.com/news/170294.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0