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

Two ways to configure Vue global methods

Table of contents 1. Introduction 2. The first me...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

How to change the MySQL database directory location under Linux (CentOS) system

How to change the MySQL database directory locati...

Vue custom v-has instruction to implement button permission judgment

Application Scenario Taking the background manage...

Tutorial on customizing rpm packages and building yum repositories for Centos

1 Keep the rpm package downloaded when yum instal...

Measured image HTTP request

Please open the test page in a mainstream browser...

How to create a stored procedure in MySQL and add records in a loop

This article uses an example to describe how to c...

Summary of Vue first screen performance optimization component knowledge points

Vue first screen performance optimization compone...

Methods to enhance access control security in Linux kernel

background Some time ago, our project team was he...

Javascript front-end optimization code

Table of contents Optimization of if judgment 1. ...