欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

electron打包vue项目成桌面应用

程序员文章站 2022-06-04 15:16:50
...

方法一:(适用于打包64位win10的单独.exe程序)

//打包vue项目得到dist文件夹
1.安装electron:  npm install --save-dev electron

2.package.json中添加
	{
	  "name": "my-electron-app",
	  "version": "1.0.0",
	  "description": "Hello World!", 
	  "main": "main.js",   
	  "author": "Jane Doe",
	  "license": "MIT"
	},
	"scripts": {
	   "start": "electron ."
	}

3.main.js
const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {

  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })
  mainWindow.loadFile('./dist/index.html')
  // 将网页链接打包成桌面应用
  // mainWindow.loadUrl('https://www.bilibili.com/')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

4.npm start启动运行

打包成单独.exe可运行程序
5.分别安装:
	npm install --save-dev @electron-forge/cli
	npx electron-forge import
    
6.运行:(程序打包在out文件夹中)
	npm run make

方法二:(适用于打包多平台linx,windows,32位,64位,非单独运行.exe程序)

electron打包vue项目成桌面应用

1.安装:npm install --save-dev electron-packager 

2.package.json文件
{
  "name": "myapp",  //打包完成的程序名称
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron-forge start",
    "package": "electron-packager ./ --all --out ./out --electron-version=15.1.1 --overwrite --icon=./login.ico  --ignore=node_modules "
  },
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "@electron-forge/cli": "^6.0.0-beta.61",
    "electron": "^15.1.1",
    "electron-packager": "^15.4.0"
  }
}

3.运行命令:npm run package