Detailed code for adding electron to the vue project

Detailed code for adding electron to the vue project

1. Add in package.json

 "main": "electron.js",

In "scripts": { add:

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

In "dependencies": { add:

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

In "devDependencies": { add:

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

2.vue.config.js

In devServer: {modify:

// open: true, (It was not commented out before, comment it out now)

In proxy: {modify

 '/api': {
        target: "electron-renderer", (only this place is changed, before it was target: 'http://localhost',)
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }

3. Add to main.js:

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

4. Add electron.js at the same level as vue.config.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. Add preload.js at the same level as vue.config.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. Package the project first

npm run dev

Then run

npm run package

Please add a description of the image

Please add a description of the image

This is the end of this article about adding electron to the vue project. For more relevant vue adding electron content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Sample code for making desktop applications with vue + Electron
  • How to start a Vue.js project
  • The implementation idea of ​​adding startup loading animation to the electron-vue project

<<:  How to run MySQL in Docker environment and enable Binlog to configure master-slave synchronization

>>:  MySQL slow query and query reconstruction method record

Recommend

HTML table tag tutorial (35): cross-column attribute COLSPAN

In a complex table structure, some cells span mul...

Linux five-step build kernel tree

Table of contents 0. The kernel tree that comes w...

Explaining immutable values ​​in React

Table of contents What are immutable values? Why ...

Example of how to upload a Docker image to a private repository

The image can be easily pushed directly to the Do...

MySQL foreign key constraint (FOREIGN KEY) case explanation

MySQL foreign key constraint (FOREIGN KEY) is a s...

VMware virtual machine three connection methods example analysis

NAT In this way, the virtual machine's networ...

Mysql anonymous login cannot create a database problem solution

Frequently asked questions Access denied for user...

Detailed explanation of how to use the Vue license plate search component

A simple license plate input component (vue) for ...

Summary of MySQL usage specifications

1. InnoDB storage engine must be used It has bett...

JavaScript implements cool mouse tailing effects

After watching this, I guarantee that you have ha...

How to monitor array changes in JavaScript

Preface When introducing defineProperty before, I...

Detailed example of using if statement in mysql stored procedure

This article uses an example to illustrate the us...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

How to turn local variables into global variables in JavaScript

First we need to know the self-calling of the fun...