【Electron】总结:如何创建Electron+Element Plus的项目

目录

一、准备环境

二、创建Element项目

三、添加依赖

四、配置页面

五、安装Element-plus

六、配置页面

七、生成安装包

八、增加支持TypeScript


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

一、准备环境

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

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

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

二、创建Element项目

  并使用Electron Forge的 Vite template.

npm init electron-app@latest my-vue-app -- --template=vite

三、添加依赖

npm install vue
npm install --save-dev @vitejs/plugin-vue

四、配置页面

根目录/index.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

<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

import { createApp } from 'vue';
import App from './App.vue';

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

根目录/ vite.renderer.config.mjs

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

npm install element-plus

六、配置页面

src/renderer.js

// 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 

<!-- 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>

致此完成。启动

七、生成安装包

electron-builder

yarn add electron-builder --dev
npm run electron-builder --save-dev

Electron的package.json配置

{
  "name": "elec",
  "version": "1.0.0",
  "description": "Hello World!",
  "main": "main.js",
  "author": "Jane Doe",
  "license": "MIT",
  "scripts": {
    "test": "test",
    "start": "electron .",
    "build": "npx electron-builder --win --x64"
  },
  "devDependencies": {
    "electron": "^33.2.0",
    "electron-builder": "^33.2.0"
  },
  "build": {
    "productName": "MyApp",
    "directories": {
      "output": "out"
    },
    "win": {
      "icon": "./view/img/logo.ico",//logo 256*256
      "target": "nsis"
    },
    "nsis": {
      "oneClick": false,
      "allowElevation": true,
      "allowToChangeInstallationDirectory": true,//用户可选择安装路径
      "installerIcon": "./view/img/logo.ico",
      "uninstallerIcon": "./view/img/logo.ico",
      "installerHeaderIcon": "./view/img/logo.ico",
      "createDesktopShortcut": true,
      "createStartMenuShortcut": true,
      "shortcutName": "我的应用"
    }
  }
}

八、增加支持TypeScript

 1、安装 TypeScript:

npm install -g typescript

2、配置 tsconfig.json 文件:

在项目根目录下创建 tsconfig.json 文件,这个文件用于配置 TypeScript 编译器选项。你可以通过命令行工具自动生成一个基本配置文件:

tsc --init

3、安装 Electron 类型定义

为了获得更好的开发体验,包括类型检查和自动完成功能,你应该安装 Electron 的类型定义文件。运行以下命令来安装: 

npm install @types/electron --save-dev

首先,你需要安装 Node.js 和 npm。然后,可以按照以下步骤构建你的项目: 1. 创建项目文件夹并进入该文件夹: ``` mkdir my-project cd my-project ``` 2. 初始化项目: ``` npm init -y ``` 3. 安装 Vite: ``` npm install vite --save-dev ``` 4. 安装 Vue.js: ``` npm install vue@next --save ``` 5. 安装 TypeScript: ``` npm install typescript --save-dev ``` 6. 安装 Electron: ``` npm install electron --save-dev ``` 7. 安装 Element Plus: ``` npm install element-plus --save ``` 8. 安装 Vuex: ``` npm install vuex --save ``` 9. 创建 TypeScript 配置文件: ``` npx tsc --init ``` 10. 创建主进程和渲染进程的 Electron 入口文件: ``` // 主进程入口文件 main.js const { app, BrowserWindow } = require('electron') function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) win.loadFile('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() } }) ``` ``` // 渲染进程入口文件 src/renderer/main.ts import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' createApp(App).use(router).use(store).use(ElementPlus).mount('#app') ``` 11. 创建 Vue 组件,例如 App.vue: ``` <template> <div id="app"> <router-view /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ name: 'App' }) </script> ``` 12. 创建 Vuex store,例如 store/index.ts: ``` import { createStore } from 'vuex' export default createStore({ state: { count: 0 }, mutations: { increment (state) { state.count++ } } }) ``` 13. 创建路由器,例如 router/index.ts: ``` import { createRouter, createWebHashHistory } from 'vue-router' import Home from '../views/Home.vue' const routes = [ { path: '/', name: 'Home', component: Home } ] const router = createRouter({ history: createWebHashHistory(), routes }) export default router ``` 14. 创建主页面组件,例如 views/Home.vue: ``` <template> <div> <h1>{{ message }}</h1> <button @click="increment">Increment</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { useStore } from 'vuex' export default defineComponent({ name: 'Home', setup () { const store = useStore() return { message: 'Hello, World!', increment () { store.commit('increment') } } } }) </script> ``` 15. 运行项目: ``` npm run dev ``` 以上就是使用 Vite、Vue.js、TypeScript、ElectronElement Plus 和 Vuex 构建项目的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值