electron环境安装略
1. electron的入口文件配置test.js, 需要在package.json 配置文件中指定main: src/test.js
- const { app, BrowserWindow } = require('electron')
-
- const createWindow = () => {
- const win = new BrowserWindow({
- width: 800,
- height: 600
- })
-
- // win.loadFile('index.html')
-
- // 下面的url为自己启动vite项目的url。
- // win.loadURL('http://127.0.0.1:28087/')
-
- // 在窗口内要展示的内容为 ./dist/index.html,即打包生成的index.html
-
- // mainWindow.loadURL(url.format({
-
- // pathname: path.join(__dirname, './dist', 'index.html'),
-
- // protocol: 'file:',
-
- // slashes: true
-
- // }));
- win.loadFile('./dist/index.html')
-
- // 自动打开调试台
-
- mainWindow.webContents.openDevTools({
-
- detach: true
-
- });
-
- }
-
- app.whenReady().then(() => {
- createWindow()
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) createWindow()
- })
- })
-
-
-
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit()
- }
- })
2. vue文件单独打包,命令: npm run build, test.js文件在src下, 然后将vue打包的dist文件,需要手动拷贝到src下
3. 打包electron 命令: electron-builder
关于python服务打包运行, 不需要部署python环境的方式
1.安装依赖, pip install pyinstaller
2.打包服务pyinstaller yourscript.py
具体参见 https://blog.csdn.net/weixin_40025666/article/details/131191945
将py打包程序嵌入到electron启动脚本中, 并将py打包的应用包依赖包复制到/app下
- app.on('ready', async () => {
- // 启动服务器exe
- try{ cmd.run(`./app/app.exe`,function(err, data, stderr){
- console.log(data)
- console.log(err)
- console.log(stderr)
- });}
- catch(e){
- console.log(e)
- }
-
-
-
- createWindow()
- })
其中cmd.run命令需要node安装
npm install node-cmd --save
并在js中引入 const cmd = require('node-cmd');
1. js本地调试, 运行node js文件即可
2. 除了node_cmd, 还可以使用组件chile_process调用exe
- var child_process = require('child_process');
- child_process.exec(`start cmd.exe /K ping 127.0.0.1`);
3.eletron需要打包带进去的文件, 以及文件路径
-
- var child_process = require('child_process');
- child_process.exec(`start cmd.exe /K ping 127.0.0.1`);
-
-
