Vue3项目-Electron构建桌面应用程序

本文主要是介绍Vue3项目-Electron构建桌面应用程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、创建Vue3项目

1、安装vue-cli
npm i -g @vue/cli#ORyarn global add @vue/cli
2、创建vue项目(vue3)
npm create vue@latest

3、创建完成后启动项目
cd project_name && npm run dev

二、electron配置

1、安装electron
npm install electron
2、安装vite-plugin-electron插件
npm install vite-plugin-electron
3、添加electron配置文件(项目/electron/index.js)
// -------------------------------<<模块导入>>-------------------------------
import { app, BrowserWindow, screen, ipcMain, Tray, Menu } from 'electron'
import path from 'path'
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'// -------------------------------<<变量声明>>-------------------------------
// 定义全局变量,获取窗口实例
const windows = {// 主窗口main: {win: null},
}const defaultMENU = 0x116; //当弹出系统菜单时//托盘区图标
var appTray = null;// -------------------------------<<函数定义>>-------------------------------
//禁用窗口自带的右键菜单
const disableContextMenu = (win) => {win.hookWindowMessage(defaultMENU, () => {win.setEnabled(false);setTimeout(() => {win.setEnabled(true);}, 20);return true;})
}// 创建主窗口
const createWindow = () => {let {width,height} = screen.getPrimaryDisplay().workArea;windows.main.win = new BrowserWindow({width: width,height: height,show: true,frame: false,movable: true,resizable: true,webPreferences: {devTools: true,// 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法nodeIntegration: true,contextIsolation: false,webSecurity: false, //是否允许渲染进程访问本地文件//允许html页面上的javascript代码访问nodejs 环境api代码的能力(与node集成的意思)}})// 开发环境 development// 是生产环境 productionif (process.env.NODE_ENV != 'development') {windows.main.win.loadFile(path.join(__dirname, "../index.html"));} else {windows.main.win.loadURL(`http://${process.env['VITE_DEV_SERVER_HOST']}:${process.env['VITE_DEV_SERVER_PORT']}` + "/index.html");if (!process.env.IS_TEST) windows.main.win.webContents.openDevTools();}disableContextMenu(windows.main.win);
}// 系统托盘设置
const setTray = () => {//设置菜单内容let trayMenu = [{label: '退出', //菜单名称click: function () { //点击事件app.quit();}}];let trayIcon = null;//设置托盘区图标if (process.env.NODE_ENV != 'development') {trayIcon = path.join(__dirname, '../favicon.ico');} else {trayIcon = path.join(__dirname, '../../public/favicon.ico');}appTray = new Tray(trayIcon);//设置菜单const contextMenu = Menu.buildFromTemplate(trayMenu);//设置悬浮提示appTray.setToolTip('xxxxxxxxx');//设置appTray.setContextMenu(contextMenu);//点击图标appTray.on('click', function () {//显示主程序if (windows.main.win) windows.main.win.show();});
}// 监听ipcMain请求
const listenIPC = () => {// 最小化ipcMain.on('min-app', () => {windows.main.win.minimize();});// 退出程序ipcMain.on('exit-app', () => {windows.main.win.close();})
}// 初始化app(Electron初始化完成时触发)
app.whenReady().then(() => {createWindow();setTray();listenIPC()// app.on('activate', () => {//     if (BrowserWindow.getAllWindows().length === 0) createWindow()// })
});app.on('window-all-closed', () => {if (process.platform !== 'darwin') app.quit()
})
4、vite.config.js配置
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron'
import path, { resolve, join } from 'path'
export default defineConfig({plugins: [vue(), electron({main: {entry: "electron/index.js"}})],resolve: {alias: {"@": resolve(__dirname, 'src'), // 这里是将src目录配置别名为 @ 方便在项目中导入src目录下的文件}},
})
5、package.json配置
{"name": "vite-project","private": true,"version": "0.0.0","main": "dist/electron/index.js", // 这里必须配置为dist/electron/index.js"scripts": {"dev": "vite","build": "vite build","preview": "vite preview"},"dependencies": {"vue": "^3.2.47","@element-plus/icons-vue": "^2.1.0","axios": "^1.3.4","echarts": "^5.4.3","element-plus": "^2.3.0","pinia": "^2.1.7","qs": "^6.11.1","vue-draggable-resizable": "^2.3.0","vue-router": "^4.2.4"},"devDependencies": {"@vitejs/plugin-vue": "^4.1.0","electron": "^19.0.10","vite": "^4.2.0","vite-plugin-electron": "^0.8.3","less": "^4.1.3","less-loader": "^11.1.0"}
}

三、项目其他配置

1、main.js配置
// 模块导入
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
import { createPinia } from "pinia";
import VueDraggableResizable from 'vue-draggable-resizable/src/components/vue-draggable-resizable.vue'
import 'vue-draggable-resizable/dist/VueDraggableResizable.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import ModelAssembly from './components/common/ModelAssembly.vue';
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'// 创建APP
const app = createApp(App);// 创建Pinia
const pinia = createPinia();//使el-input自动聚焦 所有页面都可使用 v-focus使用
app.directive('focus', {mounted: (el) => el.querySelector('input').focus()
})// 注册全局组件
app.component('ModelAssembly', ModelAssembly)//注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {app.component(key, component) //注册组件图标
}app.component('vue-draggable-resizable', VueDraggableResizable)
app.use(router);
app.use(pinia);
app.use(ElementPlus);
app.mount("#app");
2、pinia配置
import { defineStore } from "pinia"
export const useIndexStore = defineStore('indexStore', {// 其它配置项state: () => {return {};},getters: {},actions: {}
})
3、router配置
import { createRouter, createWebHashHistory } from 'vue-router';
const routes = [{path: '/', redirect: "/home"},{path: '/home',component: () => import('@/views/Home.vue'),},];
const router = createRouter({history: createWebHashHistory(),routes
});
export default router;

这篇关于Vue3项目-Electron构建桌面应用程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090640

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled