[Electron]总结:如何创建Electron+Element Plus的项目

我将结合官网手册与AI问到的信息,直接给出步骤,与命令。

一、准备环境

首先在C盘Users,你的登录的账号名文件夹下,编辑**.npmrc**文件。添加镜像地址。

如果使用了yarn,则是.yarnrc。可以全部都配置。

bash 复制代码
npm install -g yarn
bash 复制代码
registry=https://registry.npmmirror.com
ELECTRON_MIRROR="https://npmmirror.com/mirrors/electron/"
bash 复制代码
npm config set registry https://registry.npm.taobao.org

二、创建Element项目

并使用Electron Forge的 Vite template.

bash 复制代码
npm init electron-app@latest my-vue-app -- --template=vite

三、添加依赖

bash 复制代码
npm install vue
bash 复制代码
npm install --save-dev @vitejs/plugin-vue

四、配置页面

根目录/index.html

html 复制代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World!</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/renderer.js"></script>
  </body>
</html>

src/App.vue

html 复制代码
<template>
  <h1>💖 Hello World!</h1>
  <p>Welcome to your Electron application.</p>
</template>

<script setup>
console.log('👋 This message is being logged by "App.vue", included via Vite');
</script>

src/renderer.js

javascript 复制代码
import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');

根目录/ vite.renderer.config.mjs

javascript 复制代码
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

// https://vitejs.dev/config
export default defineConfig({
  plugins: [vue()]
});

至此我们创建了基于Vue3的Electron项目。

2-4步骤,参考自官网How to create an Electron app with Vue and Electron Forge

五、安装Element-plus

bash 复制代码
npm install element-plus

六、配置页面

src/renderer.js

javascript 复制代码
// src/renderer.js
import { createApp } from 'vue';
import App from './App.vue';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';

const app = createApp(App);
app.use(ElementPlus);
app.mount('#app');

src/App.vue

html 复制代码
<!-- src/App.vue -->
<template>
  <div id="app">
    <h1>Hello, Electron Forge with Vue 3 and Element Plus!</h1>
    <el-button type="primary">Primary Button</el-button>
  </div>
</template>

<script>
export default {
  name: 'App'
};
</script>

致此完成。启动

相关推荐
朦胧之几秒前
页面白屏卡住排查方法
前端·javascript
用户59360874140几秒前
Playwright 黑魔法:用 ClipboardEvent 绕过 React 富文本编辑器
前端
石山岭32 分钟前
自己动手写了一个 Android 虚拟定位 App:GPSSimulate 技术实
android·前端
犇驫聊AI1 小时前
Chrome DevTools MCP + Claude Code 自定义skills生成接口代码生成器
前端·javascript
kyriewen1 小时前
别再这样写 async/await 了:我在 Code Review 中见过最多的 8 个错误
前端·javascript·面试
hoLzwEge1 小时前
node-linker VS shamefully-hoist
前端·前端框架
袋鱼不重1 小时前
解决 Web 端图片预览与下载颜色不一致的一种工程方案
前端·后端
风止何安啊1 小时前
教你用 JS + AI 实现简单的爬虫,零门槛爬取网页信息
前端
cidy_981 小时前
codebase-memory-mcp 新手完全教程:让 AI 真正「理解」你的代码库
前端
牛奶2 小时前
HTTPS你不知道的事
前端·https·浏览器