vue2.0+electron生成.exe文件
1.安装插件
cnpm install electron --save-dev
cnpm install electron-packager --save-dev
2.在package.json中增加指令如图:
"name": "ticketing_client",
"version": "1.0.0",
"description": "A Vue.js project",
"private": true,
"main": "electron.js",
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
"unit": "jest --config test/unit/jest.conf.js --coverage",
"e2e": "node test/e2e/runner.js",
"test": "npm run unit && npm run e2e",
"build": "node build/build.js",
"electron_dev": "npm run build && electron build/electron.js",
"electron_build": "electron-packager ./dist/ --platform=win32 --arch=x64 --icon=./src/assets/favicon.ico --overwrite"
},
3.在build和dist文件夹中引入一个js文件并将package.json复制进入dist文件夹
其中electron.js内容为
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
// icon:'../src/assets/logo.ico',
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.setMenu(null)//可以使生成的exe没有菜单栏
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// 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.whenReady().then(() => {
createWindow()
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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
再解释一下electron.js:
4.注意修改build文件夹下的utils.js和config文件夹下的index.js
5.最后只用运行 cnpm run electron_build即可。
6.如图
7.注意
不要单独发送exe这样会导致文件运行报错,一定要将生成的文件夹一起发送,再运行exe。