位置: IT常识 - 正文

(二) Vue3 + Element-Plus 实现动态菜单栏

编辑:rootadmin
(二) Vue3 + Element-Plus 实现动态菜单栏 系列文章目录系列介绍:Vue3 + Vite + TS 从零开始学习项目搭建:(一) Vue3 + Vite + TS 项目搭建实现动态菜单栏:(二) Vue3 + Element-Plus 实现动态菜单栏实现动态面包屑:(三) Vue3 + Element-Plus 实现动态面包屑实现动态标签页:(四) Vue3 + Element-Plus 实现动态标签页实现动态主题色切换(demo):(五) Vue3 + Element-Plus 实现动态主题色切换踩坑记录(持续更新):(六) Vue3 踩坑记录文章目录系列文章目录一、引入依赖二、目录结构三、核心代码1. auth-api.js2. permission.js3.sidebar.vue4.router.js四、最终效果一、引入依赖

推荐整理分享(二) Vue3 + Element-Plus 实现动态菜单栏,希望有所帮助,仅作参考,欢迎阅读内容。

(二) Vue3 + Element-Plus 实现动态菜单栏

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

开始前请确保已经安装以下依赖:

VueX$ npm i vuex --saveVue-Router$ npm i vue-router --saveNProgress$ npm i nprogress --save二、目录结构|-src -- 主目录---|api -- Ajax请求统一存放目录------|auth-api.js -- 路由数据获取接口---|js -- JS脚本------|permission.js -- NProgress进度条数据处理---|layout -- 页面布局组件------|sidebar.vue -- 侧边栏布局组件---|store -- VueX------|router.js -- 路由全局常量三、核心代码1. auth-api.jsimport request from '@/js/request'export default { routers(data) { return request.post('/routers', data) }}2. permission.jsimport router from '../router'import store from '../store'import NProgress from 'nprogress'import 'nprogress/nprogress.css'import { getCookie } from './cookie'import Layout from '../layout/index.vue'import ParentView from '../components/ParentView/index.vue'NProgress.configure({ showSpinner: false })const whiteList = ['/login', '/register']router.beforeEach((to, from, next) => { NProgress.start() if (getCookie()) { if (to.path === '/login') { next({ path: '/' }) NProgress.done() } else { if (store.state.user.menus.length === 0) { store.dispatch('GetInfo').then(res => { const menuIds = res.data.menuIds store.dispatch('GenerateRoutes', menuIds).then(routes => { filterRoutes(routes) routes.forEach(route => { router.addRoute(route) }) next({ ...to, replace: true }) }) }).catch(() => { next() }) } else { next() } } } else { if (whiteList.indexOf(to.path) !== -1) { next() } else { next(`/login?redirect=${to.fullPath}`) NProgress.done() } }})router.afterEach(() => { NProgress.done()})const filterRoutes = (routes) => { const accessRoutes = routes.filter(route => { let modules = import.meta.glob('../views/**/*.vue') if (route.component) { if (route.component === 'ParentView') { route.component = ParentView } else if (route.component === 'Layout') { route.component = Layout } else { route.component = modules[`../views/${route.component}.vue`] } } if (route.children && route.children.length) { filterRoutes(route.children) } return true }) return accessRoutes}3.sidebar.vue<template> <el-aside width="210px" class="aside-wrapper"> <el-scrollbar> <el-menu :default-active="route.path" mode="vertical" :collapse-transition="false" router class="menu-wrapper"> <el-sub-menu v-for="menu in menus" :key="menu.path" :index="menu.path"> <template #title> <el-icon><component :is="menu.meta.icon" /></el-icon> <span>{{ menu.name }}</span> </template> <el-menu-item v-for="child in menu.children" :key="child.path" :index="child.path"> <template #title> <el-icon><component :is="child.meta.icon" /></el-icon> <span>{{ child.name }}</span> </template> </el-menu-item> </el-sub-menu> </el-menu> </el-scrollbar> </el-aside></template><script lang="ts" setup>import { computed } from 'vue'import { useStore } from 'vuex'import { useRoute } from 'vue-router'const store = useStore()const route = useRoute()const menus = store.state.router.accessRoutes</script>4.router.jsimport { authApi } from '@/api'import constantRoutes from '@/router/routes'const router = { state: { routes: [], accessRoutes: [] }, mutations: { SET_ROUTES: (state, routes) => { state.routes = routes }, SET_ACCESSROUTES: (state, accessRoutes) => { state.accessRoutes = accessRoutes } }, actions: { GenerateRoutes({ commit }, roleIds) { return new Promise(resolve => { authApi.routers(roleIds).then(res => { const accessRoutes = res.data filterRoutes('', accessRoutes) const routes = constantRoutes.concat(accessRoutes) commit('SET_ROUTES', routes) commit('SET_ACCESSROUTES', accessRoutes) resolve(routes) }) }) } }}const filterRoutes = (path, routes) => { routes.forEach(route => { const routePath = route.path if (route.parentId !== '0') { route.path = path + "/" + routePath } if (route.children && route.children.length) { filterRoutes(routePath, route.children) } })}export default router四、最终效果

本文链接地址:https://www.jiuchutong.com/zhishi/285057.html 转载请保留说明!

上一篇:Linux basename命令的使用详解(linux-base)

下一篇:石楠花丛中的一只欧亚红松鼠,英格兰 (© Christopher Drabble/Alamy)(石楠花 开花)

  • 出口货物退税是指
  • 运输费用的增值税税率
  • 个人社保部分公司承担可以入费用吗
  • 预提车间租入固定资产租金800元
  • 当期计提加计抵减额如何确定
  • 应税职工福利费在应纳税所得额里面吗
  • 明细分类核算的方法分为
  • 职工福利费劳保用品
  • 房地产企业融资存在的问题
  • 增值税起征点有啥用
  • 自建厂房销售
  • 一般纳税人确认收入含税吗
  • 企业与个人租赁合同范本
  • 销售佣金税点
  • 一月份开的票可以算到12月吗
  • 小微企业附加税优惠政策2023
  • 金银首饰的消费税税务处理
  • 建筑业跨区预缴增值税未预缴
  • 利润表中利润归还投资
  • 预计产品质量保证损失是什么意思
  • 超市预付卡过了还能退吗
  • 公司注销注销顺序
  • 3070和3070ti性能差多少
  • 员工离职一次性结清工资
  • mac双系统删除window后怎么重新恢复
  • 深度操作系统的窗口管理器
  • 英雄联盟中该如何加好友
  • 腾讯电脑管家中的软件市场打不开
  • 质权的成立时间为
  • 投标保证金怎么退还流程
  • uniapp使用高德地图创建多边形
  • 补缴税款计入什么科目
  • 耕地使用税的征收标准
  • TypeError: Cannot read properties of null (reading ‘insertBefore‘) vue项目报错
  • vue3中props
  • 未认证用户是什么意思
  • 空调采购及安装税率
  • 稽查补缴所得税分录
  • 在国内如何用ChatGPT
  • vue知识点汇总
  • 技术和商业模式怎么写
  • java泛型方法的使用
  • db2数据库性能调整和优化
  • 新办企业发票核定及申领
  • 发票含税和不含税的区别
  • 物业公司物业费计入什么会计科目
  • 出口货物当月销售当月就要开票吗
  • 税控盘买了怎么用
  • 交易性金融资产是什么意思
  • 油卡预付卡发票能入费用吗怎么入账
  • 进口货物如何入账
  • 预收账款怎么处理
  • 投标保证金退还
  • 返利开红字发票怎么做账
  • 金税盘可以用热点吗
  • 金蝶软件版本号有哪些
  • 委托加工业务经常出现的涉税错误有哪些
  • bitronix 连接 MySQL 出现MySQLSyntaxErrorException 的解决方法
  • mysql优化sql有哪些
  • centos安装完成后重启进不去
  • 为什么要淘汰相关性高的指标
  • RegSrvc.exe - RegSrvc是什么进程 有什么用
  • sbdrvdet.exe - sbdrvdet是什么进程 有什么用
  • win7更改电脑设置在哪里
  • win8 休眠
  • perl 教程
  • shell脚本的fi
  • python中str的用法
  • unity3d入门视频教程
  • react.development.js
  • javascript怎么用
  • js debounce实现
  • javascript获取数据类型
  • javascript核心技术
  • 自制基质
  • 国家税务局内蒙古税务
  • 河南网上税务申报步骤
  • 何为价外费用
  • 国家税务总局关于进一步优化营改增纳税服务工作的通知
  • 辽宁网上税务
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设