前段时间有分享一款electron31+vue3桌面端聊天系统。这次带来全新研发的vite5+electron31+element-plus
中后台前端解决方案ElectronViteAdmin。
技术栈
- 编辑器:vscode
- 框架技术:vite^5.3.4+vue^3.4.31+vue-router^4.4.0
- 跨端框架:electron^31.3.0
- UI组件库:element-plus^2.7.8
- 状态管理:pinia^2.2.0
- 国际化方案:vue-i18n@9
- 图表组件:echarts^5.5.1
- markdown编辑器:md-editor-v3^4.18.0
- 模拟数据:mockjs^1.1.0
- 打包工具:electron-builder^24.13.3
- electron+vite桥接插件:vite-plugin-electron^0.28.7
vite5-electron-admin
内置4种通用布局模板,支持i18n国际化、动态路由权限,整合了表格、表单、图表、列表、编辑器等业务模块。
项目框架目录
整个项目采用vue3 setup
语法编码,整合vite.js+electronjs
跨平台技术。
目前electron-viteadmin管理系统已经发布到我的原创作品集。
https://gf.bilibili.com/item/detail/1106734011
electron31主进程配置
/**
* electron主线程配置
* @author andy
*/
import { app, BrowserWindow } from 'electron'
import { WindowManager } from '../src/windows/index.js'
// 忽略安全警告提示 Electron Security Warning (Insecure Content-Security-Policy)
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true
const createWindow = () => {
let win = new WindowManager()
win.create({isMajor: true})
// 系统托盘管理
win.trayManager()
// 监听ipcMain事件
win.ipcManager()
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if(BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', () => {
if(process.platform !== 'darwin') app.quit()
})
入口文件main.js
import { createApp } from 'vue'
import './style.scss'
import App from './App.vue'
import { launchApp } from '@/windows/actions'
// 引入路由和状态配置
import Router from './router'
import Pinia from './pinia'
// 引入插件
import Plugins from './plugins'
launchApp().then(config => {
if(config) {
window.config = config
}
// 初始化app应用实例
createApp(App)
.use(Router)
.use(Pinia)
.use(Plugins)
.mount('#app')
})
electron31-admin公共模板
提供了4种通用布局模板。也可以根据需要定制化模板。
/**
* 通用布局模板
* @author Andy Q:282310962
*/
<script setup>
import { appState } from '@/pinia/modules/app'
// 引入布局模板
import Classic from './template/classic/index.vue'
import Columns from './template/columns/index.vue'
import Vertical from './template/vertical/index.vue'
import Horizontal from './template/horizontal/index.vue'
const appstate = appState()
const LayoutMap = {
'classic': Classic,
'columns': Columns,
'vertical': Vertical,
'horizontal': Horizontal
}
</script>
<template>
<div class="vuadmin__container" :style="{'--themeSkin': appstate.config.skin}">
<component :is="LayoutMap[appstate.config.layout]" />
</div>
</template>
electron-admin国际化配置
采用 vue-i18n
国际化解决方案,支持中英文/繁体语言。
/**
* 国际化配置
* @author YXY
*/
import { createI18n } from 'vue-i18n'
import { appState } from '@/pinia/modules/app'
// 引入语言配置
import enUS from './en-US'
import zhCN from './zh-CN'
import zhTW from './zh-TW'
// 默认语言
export const langVal = 'zh-CN'
export default async (app) => {
const appstate = appState()
const lang = appstate.lang || langVal
appstate.setLang(lang)
const i18n = createI18n({
legacy: false,
locale: lang,
messages: {
'en': enUS,
'zh-CN': zhCN,
'zh-TW': zhTW
}
})
app.use(i18n)
}
vue3封装动态化图表Hooks
/**
* 动态图表Hook
*/
import { onMounted, onBeforeUnmount, ref } from 'vue'
import * as echarts from 'echarts'
import elementResizeDetectorMaker from 'element-resize-detector'
export function useEcharts(el, options) {
let chartEl
let chartRef = ref(null)
let erd = elementResizeDetectorMaker()
const resizeHandle = () => {
chartEl && chartEl.resize()
}
onMounted(() => {
if(el?.value) {
chartEl = echarts.init(el.value)
chartEl.setOption(options)
chartRef.value = chartEl
}
erd.listenTo(el.value, resizeHandle)
})
onBeforeUnmount(() => {
chartEl.dispose()
erd.removeListener(el.value, resizeHandle)
})
return chartRef
}
electron-admin自定义路由菜单
调用非常简单。
<Menus :rootRouteEnable="false" />
<Menus rootRouteEnable :dark="true" />
<Menus mode="horizontal" :dark="true" />
<script setup>
import { ref, computed } from 'vue'
import { isObject, isArray, isImg } from '@/utils'
import { appState } from '@/pinia/modules/app'
import { useRoutes } from '@/hooks/useRoutes'
const props = defineProps({
// 菜单模式(vertical|horizontal)
mode: { type: String, default: 'vertical' },
// 是否开启一级路由菜单
rootRouteEnable: { type: Boolean, default: true },
// 是否暗黑模式
dark: { type: Boolean }
})
import Submenu from './submenu.vue'
// 引入主路由表
import routes from '@/router/modules/main.js'
const appstate = appState()
const { route, getActiveRoute, getCurrentRootRoute, getTreeRoutes } = useRoutes()
const activeRoute = computed(() => getActiveRoute(route))
const rootRoute = computed(() => getCurrentRootRoute(route))
const treeRoutes = computed(() => getTreeRoutes(routes))
const filterRoutes = computed(() => {
if(props.rootRouteEnable) {
return treeRoutes.value
}
// 过滤一级路由菜单
return treeRoutes.value.find(item => item.path === rootRoute.value && item.children)?.children
})
</script>
<template>
<div class="vu__menubar" :class="{'is-dark': dark, 'is-collapsed': mode == 'vertical' && appstate.config.collapsed}">
<el-menu class="vu__menus" :default-active="activeRoute" :mode="mode" :collapse="appstate.config.collapsed">
<Submenu
v-for="route in filterRoutes"
:key="route.path"
:item="route"
:rootRoute="rootRoute"
:rootRouteEnable="rootRouteEnable"
/>
</el-menu>
</div>
</template>
基于electron31+vue3+element-plus实战开发桌面端后台系统就分享到这里。
https://segmentfault.com/a/1190000045102738
https://segmentfault.com/a/1190000044984067
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。