项目前提:公司用Vue开发了个项目,部署到现场后要求打包成exe应用。
**版本: Node版本 16.13.2 Electron版本 13.6.9**
1、在项目中集成Electron
- Vue项目添加Electron-builder打包工具, 添加成功后src文件夹下生成background.js文件
vue add electron-builder
- vue.config.js增加配置(打包后不加密)
pluginOptions: {
electronBuilder: {
builderOptions: {
nsis: {
allowToChangeInstallationDirectory: true,
oneClick: false
},
electronDownload: {
mirror: "https://npm.taobao.org/mirrors/electron/"
},
win: {
target: "nsis",
icon: './build/logo.ico', // exe图标 ico要求256*256
},
productName: "ILTestClient", // 应用名称
asar: false //打包时不进行加密
}
}
}
可以在此目录\win-unpacked\resources\app查看打包后的文件和dist相同;
- 添加完electron-builder后 package.json文件会自动添加相关命令,如下图
在控制台执行npm run electron:build命令,打包exe(双击win-unpacked下的xxx.exe访问程序)。
要在exe程序中增加浏览器的ctrl f 功能
- 第一步
在background.js中注册ctrl f 事件,并在各个窗口中监听,直接上代码:
(遇到的问题:一开始百度的时候搜到的都是设置nodeIntegration为true后在Vue组件引入Electron就可以在Vue页面中监听到electron的事件,但是项目就不能在浏览器访问可能是因为浏览器没有node的运行环境,后面又查了一下可以通过preload.js进行预加载这样浏览器就可以正常访问了)
'use strict'
import { app, protocol, BrowserWindow, Menu, globalShortcut } from 'electron'
const path = require('path')
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
let win;
const isDevelopment = process.env.NODE_ENV !== 'production'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
async function createWindow() {
// 设置菜单为null
Menu.setApplicationMenu(null)
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
// 预加载node
preload: path.join(__dirname, '/preload.js'),
// 开启渲染进程的remote模块
enableRemoteModule: !!process.env.IS_TEST,
// enableRemoteModule: true,
// 开启渲染进程使用node
// nodeIntegration: false,
contextIsolation: false
}
})
win.on('ready-to-show', () => {
win.show()
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
win.on('closed', () => {
win = null
})
win.on('focus', () => {
globalShortcut.register('CommandOrControl+F', function () {
if (win && win.webContents) {
win.webContents.send('on-find', '')
}
})
// 注册 'ESC' 键的快捷键
globalShortcut.register('Escape', function () {
if (win && win.webContents) {
win.webContents.send('on-esc', '')
}
})
})
win.on('blur', () => {
globalShortcut.unregister('CommandOrControl+F')
globalShortcut.unregister('Escape')
})
// win.webContents.openDevTools()
// 在开发环境和生产环境均可通过快捷键打开devTools
globalShortcut.register("CommandOrControl+Shift+i", function () {
win.webContents.openDevTools()
})
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// 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()
}
globalShortcut.unregister('CommandOrControl+F')
globalShortcut.unregister('Escape')
})
app.on('activate', () => {
// 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// 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', async () => {
// if (isDevelopment && !process.env.IS_TEST) {
// // Install Vue Devtools
// try {
// await installExtension(VUEJS_DEVTOOLS)
// } catch (e) {
// console.error('Vue Devtools failed to install:', e.toString())
// }
// }
createWindow()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
preload.js文件内容
引入preload.js 我把它放在了src下
pluginOptions: {
electronBuilder: {
preload: ‘src/preload.js’,
builderOptions: {
nsis: {
allowToChangeInstallationDirectory: true,
oneClick: false,
},
extraResources: [
{
from: “disable-gpu”,
to: “.”,
filter: ["**/"],
},
],
electronDownload: {
mirror: “https://npm.taobao.org/mirrors/electron/”,
},
win: {
target: “nsis”,
icon: “./build/logo.ico”, // exe图标 ico要求256256
},
productName: “TestClient”,
asar: false, //打包时不进行加密
},
},
},
- 第二步 在Vue页面中去监听ctrl 事件什么时候触发(监听on-find事件)
mounted: {
if (window.ipcRenderer) {
// 监听搜索事件
window.ipcRenderer.on('on-find', (e, args) => {
this.searchBox = true;
this.$nextTick(() => {
this.$refs.findIpt.focus();
})
this.searchValue()
})
// 监听退出事件
window.ipcRenderer.on('on-esc', (e, args) => {
this.searchBox = false
})
}
}
- 打包electron生成exe文件目录如下
npm run electron:build