How to use vue3+ts+vite+electron to build a desktop application
1. Version background introduction
vite: ^4.2.0
vue: ^3.2.47
ts: ^4.9.3
electron: ^23.2.1
2. Process
1. Build a vite vue-ts project
yarn create vite@ vuets_electron --template vue-ts cd ./vuets_electron yarn install && yarn dev
2. Connect to electron
In order to ensure Electron can be installed normally. Create
.npmrc
in the root directory ofvuets_electron
, and set the image source of electron
# /.npmrc ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/ ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/
Installation related to electron Package
# electron yarn add -D electron # electron-builder 用于打包 yarn add -D electron-builder # electron-devtools-installer yarn add -D electron-devtools-installer # 为了保证后续步骤,这里在安装一个concurrently, yarn add concurrently
3. Electron startup
Create electron main process file main.ts: /src/main/main.ts
const { app, BrowserWindow } = require('electron') const createWindow = () => { const win = new BrowserWindow({ width: 800, height: 600, // webPreferences: { // preload: path.join(__dirname, 'preload.js') // } }) // 加载vue url视本地环境而定,如http://localhost:5173 win.loadURL('http://localhost:3000') } app.whenReady().then(() => { createWindow() app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })
Adjust startup command: package.json
1 vue startup: yarn dev
2 How to start electron? From the official electron documentation we can clearly know that electron can load URL, then we can Start vue before starting electron, and then connect the access entrance of vue to electron. Isn’t it enough to start electron at the same time~
3 Don’t forget to set the entry file~~~
{ "name": "vuets_electron", "private": true, "version": "1.0.0", // +++ 增加入口 "main": "src/main/main.js", // +++ "scripts": { "dev": "vite", "build": "vue-tsc && vite build", "preview": "vite preview", // +++ 设置electron开发启动命令 "electron:dev": "concurrently \"yarn dev\" \"electron .\"" // +++ } // ... // 其它配置 }
At this point, we can see the familiar electron page by running yarn electron:dev
4. Electron packaging
1 Packaging vue
2 Connect the vue entry file to electron
3 Package electron so that we can get the complete installation package
# package.json { "name": "vuets_electron", "private": true, "version": "1.0.0", "main": "src/main/main.js", // +++ "scripts": { "dev": "vite", "build": "vue-tsc && vite build", "preview": "vite preview", "electron:dev": "concurrently \"yarn dev\" \"electron .\"", // +++ 设置electron打包命令 "electron:build": "yarn build && electron-builder" // +++ } // ... // 其它配置 // +++ 打包相关设置 "build": { "appId": "ink.bennent_g.demo", "directories": { "output": "output" }, // 其它的build相关设置,可参考 electron-builder官方文档 } }
vite.config.ts adjustment
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue(), ], // +++ base: './', // +++ server: { port: 3000 } // ... })
src/main/main.ts Adjustment
const { app, BrowserWindow } = require('electron') const createWindow = () => { const win = new BrowserWindow({ width: 800, height: 600, // webPreferences: { // preload: path.join(__dirname, 'preload.js') // } }) // +++ 开发环境与打包后加载vue入口文件有所区别 // and load then index.html or the app if(process.env.npm_lifecycle_event === 'electron:dev') { win.loadURL('http://localhost:3000') win.webContents.openDevTools() } else { win.loadFile('dist/index.html') } // +++ } app.whenReady().then(() => { createWindow() app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })
5. Project directory sorting
In order to clearly distinguish the main process and rendering of electron process, we can organize vue-related files into the render directory
Move vue-related files, please be sure to pay attention to the path issues of vue-related references
The following is my directory structure, you can refer to
vuets_electron // 项目名称 │ —— node_modules │ —— dist // vue打包目录 │ —— output // electron打包目录 │ —— public │ —— .npmrc │ —— package.json │ —— vite.config.ts │ —— tsconfig.json │ └─── src // 开发相关目录 │ │ main.ts // vue默认入口文件 │ └───assets // 静态资源目录 │ │ ... │ └───main // electron主进程目录 │ │ main.ts │ └───render // 渲染进程目录即vue相关目录结构 │ │ router │ │ views │ │ ...
At this point, our electron development framework is completed, and we can happily write code~
3. Packaging home page loading blank issue (supplementary)
If the project uses vue-router, if we run the exe after building, we will find the homepage
white screen
. This is because electron only supportshash
mode. If you usecreateWebHistory()
Create route, can be changed tocreateWebHashHistory()
const router = createRouter({ // history: createWebHistory(), // 修改为 history: createWebHashHistory(), routes })
The above is the detailed content of How to use vue3+ts+vite+electron to build a desktop application. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Vue3+TS+Vite development skills: How to perform SEO optimization SEO (SearchEngineOptimization) refers to optimizing the structure, content and keywords of the website to rank it higher in search engines, thereby increasing the website's traffic and exposure. . In the development of modern front-end technologies such as Vue3+TS+Vite, how to optimize SEO is a very important issue. This article will introduce some Vue3+TS+Vite development techniques and methods to help

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

To achieve partial refresh of the page, we only need to implement the re-rendering of the local component (dom). In Vue, the easiest way to achieve this effect is to use the v-if directive. In Vue2, in addition to using the v-if instruction to re-render the local dom, we can also create a new blank component. When we need to refresh the local page, jump to this blank component page, and then jump back in the beforeRouteEnter guard in the blank component. original page. As shown in the figure below, how to click the refresh button in Vue3.X to reload the DOM within the red box and display the corresponding loading status. Since the guard in the component in the scriptsetup syntax in Vue3.X only has o

Vue3+TS+Vite development skills: How to carry out front-end security protection. With the continuous development of front-end technology, more and more companies and individuals are beginning to use Vue3+TS+Vite for front-end development. However, the security risks that come with it have also attracted people's attention. In this article, we will discuss some common front-end security issues and share some tips on how to protect front-end security during the development process of Vue3+TS+Vite. Input validation User input is often one of the main sources of front-end security vulnerabilities. exist

Vue3+TS+Vite development skills: How to optimize cross-domain requests and network requests Introduction: In front-end development, network requests are a very common operation. How to optimize network requests to improve page loading speed and user experience is one of the issues that our developers need to think about. At the same time, for some scenarios that require sending requests to different domain names, we need to solve cross-domain issues. This article will introduce how to make cross-domain requests and optimization techniques for network requests in the Vue3+TS+Vite development environment. 1. Cross-domain request solution

Vue implements the blog front-end and needs to implement markdown parsing. If there is code, it needs to implement code highlighting. There are many markdown parsing libraries for Vue, such as markdown-it, vue-markdown-loader, marked, vue-markdown, etc. These libraries are all very similar. Marked is used here, and highlight.js is used as the code highlighting library. The specific implementation steps are as follows: 1. Install dependent libraries. Open the command window under the vue project and enter the following command npminstallmarked-save//marked to convert markdown into htmlnpmins

Vue3+TS+Vite development tips: How to encrypt and store data. With the rapid development of Internet technology, data security and privacy protection are becoming more and more important. In the Vue3+TS+Vite development environment, how to encrypt and store data is a problem that every developer needs to face. This article will introduce some common data encryption and storage techniques to help developers improve application security and user experience. 1. Data Encryption Front-end Data Encryption Front-end encryption is an important part of protecting data security. Commonly used

vue3+ts+axios+pinia realizes senseless refresh 1. First download aiXos and pinianpmipinia in the project--savenpminstallaxios--save2. Encapsulate axios request-----Download js-cookienpmiJS-cookie-s//Introduce aixosimporttype{AxiosRequestConfig ,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess
