头图

前段时间有分享一款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公共模板

3ba4e7a7e9dd6af6e811dec4c5e42f7a_1289798-20240818235558527-1299128019.png

提供了4种通用布局模板。也可以根据需要定制化模板。

88f0659f5e343b440c99ec3be244ab44_1289798-20240818235329984-535715085.png

/**
 * 通用布局模板
 * @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国际化配置

2c191e70174cfe63d41bdf477299810b_1289798-20240819000104379-773468752.png

采用 vue-i18n 国际化解决方案,支持中英文/繁体语言。

ab2d886e5be8ba5fa950697d3e5a7982_1289798-20240819000305055-156995693.png

/**
 * 国际化配置
 * @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

3de3bd90cdd7562433314d1d71dce39a_1289798-20240819000900561-1398574801.png

0b8e25d3ab01602ae0b0aac4fdd6b2d8_1289798-20240819000818726-1825693871.png

1b76f6e7b89418bc5bcfdcbe45f4461a_1289798-20240819000932682-1146835271.png

/**
 * 动态图表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自定义路由菜单

cbe753d242e3befda4f6c234eec5ed1c_1289798-20240819001802836-1016591045.png
7cf59a7a89a4c52859ddef260691eeb5_1289798-20240819001542618-980603872.png
aedc78265f02284ad05c64cd700ffc4b_1289798-20240819001633707-228666262.png
add607e8cd48d70e8ffd6f267a25e06a_1289798-20240819001710646-757385158.png

调用非常简单。

<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


xiaoyan2017
765 声望314 粉丝

web前端开发爱好者,专注于前端h5、jquery、vue、react、angular等技术研发实战项目案例。