当前位置: 首页 > news >正文

创建一个electron-vite项目

前置条件:非常重要!!!

npm:

npm create @quick-start/electron@latest

yarn:

yarn create @quick-start/electron

然后进入目录,下载包文件,运行项目

到以上步骤,你已经成功运行起来一个 electron项目。

拓展知识:

接下来 我们可以给他删除不必要的文件,添加ui组件库,添加router管理,添加axios请求。

1:删除不需要的内容。

删除src/renderer/assets和components下面的所有内容

删除src/renderer/main.js的样式引入

删除App.vue文件的内容,写上测试内容

接着运行之后:

2:下载使用element-plus

 打开官方ui文档:安装 | Element Plus

下载ui库

 npm install element-plus --save

在src/renderer/src/main.js添加代码

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'const app = createApp(App)app.use(ElementPlus)
app.mount('#app')

然后再App.vue文件中放入按钮进行测试

3:下载vue-router

官方地址:安装 | Vue Router

npm install vue-router@4

在src/renderer/src下新建views文件夹 再里面新建两个测试文件vue,我这里新加了Home.vue和About.vue,写上测试内容方便观看。

然后在 src/renderer/src新建router文件并且在下面设置index.js

// router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import About from '../views/About.vue';const routes = [{path: '/',name: 'Home',component: Home},{path: '/about',name: 'About',component: About}
];const router = createRouter({history: createWebHistory('/'),routes
});export default router;

 最后在src/renderer/src/main.js添加router


import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import router from './router';
const app = createApp(App)app.use(ElementPlus)
app.use(router)
app.mount('#app')

 在src/renderer/src/components下添加菜单组件

<template><el-menu:default-active="activeIndex"class="el-menu-demo"mode="horizontal":ellipsis="false"@select="handleSelect"><div class="flex-grow" /><el-menu-item index="1">首页</el-menu-item><el-menu-item index="2">关于</el-menu-item></el-menu></template><script lang="ts" setup>import { ref } from 'vue'import { useRouter } from 'vue-router'const router = useRouter() // 使用 useRouter() 函数获取 router 实例const activeIndex = ref('1')const handleSelect = (key: string, keyPath: string[]) => {if (key == '1') {router.push('/')} else {router.push('/about')}}</script><style>.flex-grow {flex-grow: 1;}</style>

在App.vue添加控件测试路由是否正常

<template><navBar></navBar><router-view></router-view>
</template><script setup>
import navBar from './components/navBar.vue'
</script><style>
</style>

最后运行测试

4:下载axios

npm install axios

主要是利用线程通信:

主线程与渲染线程互相通信(渲染线程可以理解为vue页面)

示例:

主线程:src/main/index.js引入axios 发送请求

import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import axios from 'axios'//引入axios
function createWindow() {// Create the browser window.const mainWindow = new BrowserWindow({width: 900,height: 670,show: false,autoHideMenuBar: true,...(process.platform === 'linux' ? { icon } : {}),webPreferences: {preload: join(__dirname, '../preload/index.js'),sandbox: false}})mainWindow.on('ready-to-show', () => {mainWindow.show()})mainWindow.webContents.setWindowOpenHandler((details) => {shell.openExternal(details.url)return { action: 'deny' }})// HMR for renderer base on electron-vite cli.// Load the remote URL for development or the local html file for production.if (is.dev && process.env['ELECTRON_RENDERER_URL']) {mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])} else {mainWindow.loadFile(join(__dirname, '../renderer/index.html'))}
}// 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(() => {// Set app user model id for windowselectronApp.setAppUserModelId('com.electron')// Default open or close DevTools by F12 in development// and ignore CommandOrControl + R in production.// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utilsapp.on('browser-window-created', (_, window) => {optimizer.watchWindowShortcuts(window)})// IPC通信模块传给预加载脚本 调用接口 此接口可直接使用为测试接口 监听渲染线程发送的get-data事件然后返回数据ipcMain.on('get-data', async (event) => {try {//发送请求const response = await axios.get('http://jsonplaceholder.typicode.com/posts')if(response.data){//再把数据发送给渲染线程event.sender.send('dataList', response.data)}} catch (error) {console.error('Error fetching data:', error)event.sender.send('dataList', [])}})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', () => {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.

2:渲染线程,我这里是Home.vue页面 

注意:window.electron这个api是在预渲染暴漏来的 大家记得检查(src/preload/index.js)

Home.vue页面

<template><div>主页11</div><!-- 显示接收到的数据 --><ul><li v-for="val in list" :key="val.id">{{ val.title }}</li></ul>
</template><script setup>
import { ref, onMounted } from 'vue'
let list = ref([])
// // 当组件挂载时发送数据请求
window.electron.ipcRenderer.send('get-data')// 监听来自主进程的数据
window.electron.ipcRenderer.on('dataList', (e, data) => {list.value = dataconsole.log(list.value);
})
</script><style></style>

最后运行项目

到这里就恭喜你添加axios请求成功。

5:根据文章量可能后期会加Pinia

原创不易,记得转发添加链接!!!

 

相关文章:

创建一个electron-vite项目

前置条件&#xff1a;非常重要&#xff01;&#xff01;&#xff01; npm: npm create quick-start/electronlatest yarn: yarn create quick-start/electron 然后进入目录&#xff0c;下载包文件&#xff0c;运行项目 到以上步骤&#xff0c;你已经成功运行起来一个 electr…...

Codeforces Round 935 (Div. 3)A~E

A. Setting up Camp 题目分析: 有三种人&#xff0c;内向、外向、综合&#xff0c;内向必须独自一个帐篷&#xff0c;外向必须3个人一个帐篷&#xff0c;综合介于1~3人一个帐篷&#xff0c;我们发现非法情况只会存在外向的人凑不成3个人一个帐篷的情况&#xff0c;因外向不够可…...

ES: spring boot中使用ElasticsearchClient

一、依赖&#xff1a;&#xff08;要根据不同版本的ES来调整依赖,否则会报错&#xff0c;不支持太低版本的ES&#xff0c;比如7.6以下的&#xff09; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-e…...

实体框架EF(Entity Framework)简介

实体框架EF&#xff08;Entity Framework&#xff09;简介 文章目录 实体框架EF&#xff08;Entity Framework&#xff09;简介一、概述二、O/R Mapping是什么采用O/R Mapping带来哪些好处 三、Entity Framework架构3.1 下图展示了Entity Framework的整体架构3.2 Entity Framew…...

使用CUDA 为Tegra构建OpenCV

返回&#xff1a;OpenCV系列文章目录&#xff08;持续更新中......&#xff09; 上一篇&#xff1a;MultiArch与Ubuntu/Debian 的交叉编译 下一篇&#xff1a;在iOS中安装 警告&#xff1a; 本教程可能包含过时的信息。 使用CUDA for Tegra 的OpenCV 本文档是构建支持 CUD…...

YoloV8改进策略:BackBone改进|PKINet

摘要 PKINet是面向遥感旋转框的主干,网络包含了CAA、PKI等模块,给我们改进卷积结构的模型带来了很多启发。本文,使用PKINet替代YoloV8的主干网络,实现涨点。PKINet是我在作者的模型基础上,重新修改了底层的模块,方便大家轻松移植到YoloV8上。 论文:《Poly Kernel Ince…...

如何在Linux系统部署Dupal CMS结合内网穿透实现无公网IP访问web界面

文章目录 前言1. Docker安装Drupal2. 本地局域网访问3 . Linux 安装cpolar4. 配置Drupal公网访问地址5. 公网远程访问Drupal6. 固定Drupal 公网地址 正文开始前给大家推荐个网站&#xff0c;前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&a…...

小巧玲珑的SQLite

SQLite是桌面端和移动端的不错选择 前言 SQLite身影无处不在。 SQLite&#xff0c;是一款轻型的数据库&#xff0c;是遵守ACID的关系型数据库管理系统&#xff0c;它的设计目标是嵌入式的&#xff0c;而且目前已经在很多嵌入式产品中使用了它&#xff0c;它占用资源非常的低&a…...

【Android 内存优化】 native内存泄漏监控方案源码分析

文章目录 前言使用效果使用apiJNI的动态注册native方法动态注册 hook的实现android_dlopen_ext和dl_iterate_phdr naive监控的实现nativeGetLeakAllocs 总结 前言 Android的native泄漏怎么检测&#xff1f;下面通过研究开源项目KOOM来一探究竟。 使用效果 未触发泄漏前的日志…...

数据结构 二叉树 力扣例题AC——代码以及思路记录

LCR 175. 计算二叉树的深 某公司架构以二叉树形式记录&#xff0c;请返回该公司的层级数。 AC int calculateDepth(struct TreeNode* root) {if (root NULL){return 0;}else{return 1 fmax(calculateDepth(root->left), calculateDepth(root->right));} } 代码思路 …...

Android 11系统启动流程

在Android 11系统启动流程中&#xff0c;系统启动主要经历了以下几个阶段&#xff1a; 引导加载程序&#xff08;Bootloader&#xff09;启动&#xff1a; 当设备加电后&#xff0c;首先运行的是ROM Bootloader&#xff0c;它负责验证操作系统映像的完整性、初始化基本硬件并加…...

python 爬取杭州小区挂牌均价

下载chrome驱动 通过chrome浏览器的 设置-帮助-关于Google Chrome 查看你所使用的Chrome版本 驱动可以从这两个地方找: 【推荐】https://storage.googleapis.com/chrome-for-testing-publichttp://npm.taobao.org/mirrors/chromedriver import zipfile import os import r…...

数据可视化-ECharts Html项目实战(3)

在之前的文章中&#xff0c;我们学习了如何创建堆积折线图&#xff0c;饼图以及较难的瀑布图并更改图标标题。想了解的朋友可以查看这篇文章。同时&#xff0c;希望我的文章能帮助到你&#xff0c;如果觉得我的文章写的不错&#xff0c;请留下你宝贵的点赞&#xff0c;谢谢。 …...

【理解机器学习算法】之Clustering算法(K-Means)

实现 K-means 聚类从零开始涉及几个关键步骤&#xff1a;初始化质心、将点分配给最近的质心、根据分配更新质心&#xff0c;以及重复这个过程直到收敛。这里是一个基本的 Python 实现&#xff1a; K-means 算法步骤&#xff1a; 初始化质心&#xff1a;从数据点中随机选择 k …...

Transformer的前世今生 day02(神经网络语言模型、词向量)

神经网络语言模型 使用神经网络的方法&#xff0c;去完成语言模型的两个问题&#xff0c;下图为两层感知机的神经网络语言模型&#xff1a; 假设词典V内有五个词&#xff1a;“判断”、“这个”、“词”、“的”、“词性”&#xff0c;且要输出P(w_next | “判断”、“这个”、…...

【Linux】多线程编程基础

&#x1f4bb;文章目录 &#x1f4c4;前言&#x1f33a;linux线程基础线程的概念线程的优缺点线程与进程的区别 线程的创建 &#x1f33b;linux线程冲突概念互斥锁函数介绍加锁的缺点 &#x1f4d3;总结 &#x1f4c4;前言 无论你是否为程序员&#xff0c;相信多线程这个词汇应…...

【地图】腾讯地图 - InfoWindow 自定义信息窗口内容时,内容 html 嵌套混乱问题

目录 需求描述问题问题代码页面展示 解决原因解决办法解决代码页面展示 代码汇总注 需求描述 腾讯地图上画点位&#xff0c;点击点位展示弹框信息 问题 问题代码 // 打开弹框 openInfoWindow(position, content) {this.infoWindow new TMap.InfoWindow({map: this.map,posit…...

Vue3、element-plus和Vue2、elementUI的一些转换

插槽 Vue3<template #default"scope"></template> <template #footer></template>Vue2<template slot-scope"scope"></template> <template slot"footer"></template>JS定义 Vue3 <script…...

Go语言gin框架中加载html/css/js等静态资源

Gin框架没有内置静态文件服务&#xff0c;但可以使用gin.Static或gin.StaticFS中间件来提供静态文件服务。 效果图如下&#xff1a; 一、gin 框架加载 Html 模板文件的方法 方式1&#xff1a;加载单个或多个html文件&#xff0c;需要指明具体文件名 r.LoadHTMLFiles("vie…...

#鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行

3 月 19 日&#xff0c;#鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行。 现场&#xff0c;深圳市南山区人民政府副区长李志娜发布《2024 年南山区支持鸿蒙原生应用发展首批政策措施清单》&#xff0c;从加强鸿蒙原生应用供给能力、推动鸿蒙原生应用产业集聚、完善鸿蒙原生…...

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销&#xff0c;平衡网络负载&#xff0c;延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

从WWDC看苹果产品发展的规律

WWDC 是苹果公司一年一度面向全球开发者的盛会&#xff0c;其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具&#xff0c;对过去十年 WWDC 主题演讲内容进行了系统化分析&#xff0c;形成了这份…...

JVM垃圾回收机制全解析

Java虚拟机&#xff08;JVM&#xff09;中的垃圾收集器&#xff08;Garbage Collector&#xff0c;简称GC&#xff09;是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象&#xff0c;从而释放内存空间&#xff0c;避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

服务器硬防的应用场景都有哪些?

服务器硬防是指一种通过硬件设备层面的安全措施来防御服务器系统受到网络攻击的方式&#xff0c;避免服务器受到各种恶意攻击和网络威胁&#xff0c;那么&#xff0c;服务器硬防通常都会应用在哪些场景当中呢&#xff1f; 硬防服务器中一般会配备入侵检测系统和预防系统&#x…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

laravel8+vue3.0+element-plus搭建方法

创建 laravel8 项目 composer create-project --prefer-dist laravel/laravel laravel8 8.* 安装 laravel/ui composer require laravel/ui 修改 package.json 文件 "devDependencies": {"vue/compiler-sfc": "^3.0.7","axios": …...

【分享】推荐一些办公小工具

1、PDF 在线转换 https://smallpdf.com/cn/pdf-tools 推荐理由&#xff1a;大部分的转换软件需要收费&#xff0c;要么功能不齐全&#xff0c;而开会员又用不了几次浪费钱&#xff0c;借用别人的又不安全。 这个网站它不需要登录或下载安装。而且提供的免费功能就能满足日常…...

动态 Web 开发技术入门篇

一、HTTP 协议核心 1.1 HTTP 基础 协议全称 &#xff1a;HyperText Transfer Protocol&#xff08;超文本传输协议&#xff09; 默认端口 &#xff1a;HTTP 使用 80 端口&#xff0c;HTTPS 使用 443 端口。 请求方法 &#xff1a; GET &#xff1a;用于获取资源&#xff0c;…...

【无标题】路径问题的革命性重构:基于二维拓扑收缩色动力学模型的零点隧穿理论

路径问题的革命性重构&#xff1a;基于二维拓扑收缩色动力学模型的零点隧穿理论 一、传统路径模型的根本缺陷 在经典正方形路径问题中&#xff08;图1&#xff09;&#xff1a; mermaid graph LR A((A)) --- B((B)) B --- C((C)) C --- D((D)) D --- A A -.- C[无直接路径] B -…...