vue项目中添加electron

1.在package.json中添加

  "main": "electron.js",

在 “scripts”: {添加:

  "package": "electron-packager ./ appName --overwrite"

在"dependencies": {添加:

   "electron-share-memory": "^1.0.1",
   "node-gyp-build": "^4.3.0",
   "vue-photo-preview": "^1.1.3",

在 “devDependencies”: {添加:

   "electron": "^15.3.1",
   "electron-packager": "^15.4.0",

2.vue.config.js
在devServer: {修改:

// open: true,(之前是没有注释掉的,现在把他注释掉)

在proxy: {修改

 '/api': {
        target: "electron-renderer",(只改了这个地方,之前是 target: 'http://localhost',)
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }

3.main.js添加:

import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
Vue.use(preview)
// 解决electron 报错
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'TRUE'

4.在vue.config.js同级添加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: 1889,
    height: 1000,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
//   mainWindow.loadFile('./dist/index.html')
  mainWindow.loadURL('http://172.16.1.155:7890/xjbd')

  // 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.

5.在vue.config.js同级添加preload.js

// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const type of ['chrome', 'node', 'electron']) {
    replaceText(`${type}-version`, process.versions[type])
  }
})

6.先把项目打包

 npm run dev

然后在运行

 npm run package

请添加图片描述

请添加图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是在 Vue 项目引入 Electron 的详细方法: 1. 首先,需要在 Vue 项目的根目录下安装 Electron: ``` npm install electron --save-dev ``` 2. 在项目根目录下创建一个 `main` 目录,并在该目录下创建一个 `index.js` 文件,用于启动 Electron: ```javascript const { app, BrowserWindow } = require('electron') function createWindow () { // 创建浏览器窗口 let win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) // 加载 Vue 项目的主页面 win.loadURL('http://localhost:8080') // 打开开发者工具 win.webContents.openDevTools() } // Electron 初始化完成后执行的回调函数 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() }) ``` 在 `package.json` 文件添加以下脚本: ```json { "scripts": { "electron": "electron main/index.js" } } ``` 3. 修改 `vue.config.js` 文件,在开发模式下执行 `electron` 命令启动 Electron: ```javascript module.exports = { // ... devServer: { before (app, server) { if (process.env.NODE_ENV === 'development') { const electron = require('child_process').spawn('npm', ['run', 'electron']) electron.stdout.on('data', data => { console.log(data.toString()) }) electron.stderr.on('data', data => { console.error(data.toString()) }) electron.on('close', code => { console.log(`Electron exited with code ${code}`) }) } } } } ``` 4. 在 `public` 目录下创建一个 `electron.js` 文件,用于判断当前是否在 Electron 运行: ```javascript window.isElectron = navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 ``` 5. 修改 `public/index.html` 文件,根据当前是否在 Electron 运行来加载不同的脚本: ```html <body> <noscript> <strong>We're sorry but my-app doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <script> if (window.isElectron) { require('./electron.js') } else { document.write('<script src="http://localhost:8080/js/chunk-vendors.js"></script><script src="http://localhost:8080/js/app.js"></script>') } </script> </body> ``` 6. 最后,在命令行执行以下命令启动 Vue 项目: ``` npm run serve ``` 在浏览器访问 `http://localhost:8080`,即可看到 Vue 项目的主页面。同时,在命令行执行以下命令启动 Electron: ``` npm run electron ``` 即可看到 Electron 窗口加载了 Vue 项目的主页面。 注意:在 Electron 使用 Vue,需要在 `webPreferences` 设置 `nodeIntegration` 为 `true`,这样才能在渲染进程使用 Node.js 模块。但是,这样也会存在一些安全问题,需要注意。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值