vue+element使用动态加载路由方式实现三级菜单页面显示的操作


Posted in Javascript onAugust 04, 2020

需要用到中间件的方式,这样就可以实现了我们想要的方式

publish-center.vue

<template>
  <router-view></router-view>
</template>
<script>
  export default {
 
  }
</script>
<el-menu :default-active="$route.path" class="el-menu-vertical-demo el-menus" @open="handleopen" @close="handleclose" @select="handleselect"
  unique-opened router v-show="!collapsed" >
  <div class="tools" @click.prevent="collapse">
   <i>|||</i>
 
   </div>
 <template v-for="(item,index) in $router.options.routes" v-if="!item.hidden">
<el-submenu :index="index+''" v-if="!item.leaf">
 <template slot="title" ><i :class="item.iconCls"></i>{{item.name}}</template>
 
 <el-menu-item-group v-for="(child,indexs) in item.children" :index="child.path" :key="child.path" v-if="!child.hidden">
      <!-- <el-menu class="xz" v-if="!child.path" :default-active="child.path"> -->
      <el-submenu :index="child.path" v-if="child.z &&!item.leaf">
       <template slot="title" class="child_title"><i :class="child.iconCls"></i>{{child.name}}</template>
       <el-menu-item v-for="(sun,i) in child.children" :index="sun.path" :key="sun.path" >
       {{sun.name}}
       </el-menu-item>
      </el-submenu>
      <!-- </el-menu> -->
      <el-menu-item :index="child.path" v-if="!child.z" :key="child.path"> {{child.name}} </el-menu-item>
  </el-menu-item-group>
  
     <!-- <el-menu-item v-if="child.path">{{child.name}}</el-menu-item> -->
 
 <!-- </el-menu-item-group> -->
   <!-- <el-menu-item v-for="sun in child.children" :index="sun.path" :key="sun.path" v-if="!sun.hidden"> {{sun.name}}</el-menu-item> -->
 
 
</el-submenu>
 <el-menu-item v-if="item.leaf&&item.children.length>0" :index="item.children[0].path"><i :class="item.iconCls"></i>{{item.children[0].name}}</el-menu-item>
 </template>
</el-menu>

路由部分:

{
   path:'/recordQuery',
   component:Home,
   name:'菜单1',
   iconCls:'el-icon-search',
   children:
   [
     { 
     path: 'carRecord', 
     z:'1',
     component: () => import('@/page/publish-center.vue'),
     name: '菜单2' , 
     children:[{ path: '/carRecord/passRecord', component: passRecord, name: '菜单' },
 
    ]
     },
     ]
},

补充知识:Vue使用addRouter()动态生成三级菜单的问题

标题记录一次vue使用addRouter动态生成三级菜单栏的问题

1.addRouter()里面需要一个固定格式的数组:例如

import { asyncRoutes, constantRoutes } from '@/router'
/* Layout */
import Layout from '@/layout'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
 if (route.meta && route.meta.roles) {
  return roles.some(role => route.meta.roles.includes(role))
 } else {
  return true
 }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles, parentModules) {
 const res = []

 parentModules.forEach(parentModule => {
  var tmp = {}
  tmp.path = parentModule.parentTargetUrl
  
  tmp.component = Layout
  tmp.name = parentModule.parentTargetUrl.substr(1)
  tmp.meta = {}
  tmp.meta.title = parentModule.parentModuleName
  tmp.meta.icon = 'dashboard'
  tmp.meta.roles = roles
  tmp.children = []
  
  parentModule.modules.forEach(module => {
   
   // XXXListPage
   var childrenTmpListPage = {}
   var childrenTmpEditPage = {}
   //childrenTmpListPage.path = '/' + module.targetName + 'ListPage'
   childrenTmpListPage.name = module.targetName + 'ListPage'
   childrenTmpListPage.meta = {}
   childrenTmpListPage.meta.title = module.moduleName
   childrenTmpListPage.meta.icon = 'dashboard'
   childrenTmpListPage.meta.roles = roles
   //childrenTmpListPage.component = resolve => require(['@/page/' + module.targetName.charAt(0).toUpperCase() + module.targetName.slice(1) + '/list_page'], resolve)
   childrenTmpListPage.path = '/' + module.targetName + 'ListPage'
   
   if(module.childrenModules != null && module.childrenModules != undefined && module.childrenModules != ""){
    childrenTmpListPage.component = Layout

   }else{
    
    childrenTmpListPage.component = resolve => require(['@/page/' + module.targetName.charAt(0).toUpperCase() + module.targetName.slice(1) + '/list_page'], resolve)
   }

   childrenTmpListPage.children = []
   // XXXEditPage
   // childrenTmpEditPage.path = '/' + module.targetName + 'EditPage'
   // childrenTmpEditPage.name = module.targetName + 'EditPage'
   // childrenTmpEditPage.meta = {}
   // childrenTmpEditPage.meta.title = module.moduleName
   // childrenTmpEditPage.meta.icon = 'dashboard'
   // childrenTmpEditPage.meta.roles = roles
   // childrenTmpEditPage.hidden = true
   // childrenTmpEditPage.component = resolve => require(['@/page/' + module.targetName.charAt(0).toUpperCase() + module.targetName.slice(1) + '/edit_page'], resolve)
    
   // 三级菜单生成
   var roleChildren = roles
   
   if(module.childrenModules != null && module.childrenModules != undefined && module.childrenModules != ""){
    module.childrenModules.forEach(module =>{
 
     var children2TmpListPage = {}
     var children2TmpEditPage = {}
     // childrenTmpListPage.path
     children2TmpListPage.path = module.targetName + 'ListPage'
     alert(children2TmpListPage.path)
     children2TmpListPage.name = module.targetName + 'ListPage'
     children2TmpListPage.meta = {}
     children2TmpListPage.meta.title = module.moduleName
     children2TmpListPage.meta.icon = 'dashboard'
     children2TmpListPage.meta.roles = roleChildren
     
     children2TmpListPage.component = resolve => require(['@/page/' + module.targetName.charAt(0).toUpperCase() + module.targetName.slice(1) + '/list_page'], resolve)
     console.log('@/page/' + module.targetName.charAt(0).toUpperCase() + module.targetName.slice(1) + '/list_page')
     childrenTmpListPage.children.push(children2TmpListPage)
     
   })
  }
   
   tmp.children.push(childrenTmpListPage)
   // tmp.children.push(childrenTmpEditPage)
  })
  res.push(tmp)
  
 })

 console.log(res)
 return res
}

const state = {
 routes: [],
 addRoutes: []
}

const mutations = {
 SET_ROUTES: (state, routes) => {
  state.addRoutes = routes
  state.routes = constantRoutes.concat(routes)
 }
}

const actions = {
 generateRoutes({ commit }, obj) {
  return new Promise(resolve => {
   let accessedRoutes = filterAsyncRoutes(asyncRoutes, obj.roles, obj.parentModules)
   commit('SET_ROUTES', accessedRoutes)
   resolve(accessedRoutes)
  })
 }
}

export default {
 namespaced: true,
 state,
 mutations,
 actions
}

三级路由重点:

componment: 二级路由不能设置为NULL,必须存在,我设置的就是Layout(引用自@/layout)

path:路径最前面不能使用"/"

以上这篇vue+element使用动态加载路由方式实现三级菜单页面显示的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jsTree树控件(基于jQuery, 超强悍)[推荐]
Sep 01 Javascript
JavaScript三元运算符的多种使用技巧
Apr 16 Javascript
JS中artdialog弹出框控件之提交表单思路详解
Apr 18 Javascript
CKEditor无法验证的解决方案(js验证+jQuery Validate验证)
May 09 Javascript
jquery判断input值不为空的方法
Jun 05 Javascript
浅谈EasyUi ComBotree树修改 父节点选择的问题
Nov 07 Javascript
React Native验证码倒计时工具类分享
Oct 24 Javascript
深入理解javascript prototype的相关知识
Sep 19 Javascript
15 分钟掌握vue-next响应式原理
Oct 13 Javascript
es6中class类静态方法,静态属性,实例属性,实例方法的理解与应用分析
Feb 15 Javascript
vue自定义指令限制输入框输入值的步骤与完整代码
Aug 30 Javascript
Element el-button 按钮组件的使用详解
Feb 01 Javascript
Vue登录拦截 登录后继续跳转指定页面的操作
Aug 04 #Javascript
vue 解决uglifyjs-webpack-plugin打包出现报错的问题
Aug 04 #Javascript
浅谈在vue-cli3项目中解决动态引入图片img404的问题
Aug 04 #Javascript
解决vuecli3中img src 的引入问题
Aug 04 #Javascript
简介JavaScript错误处理机制
Aug 04 #Javascript
vue 出现data-v-xxx的原因及解决
Aug 04 #Javascript
elementui更改el-dialog关闭按钮的图标d的示例代码
Aug 04 #Javascript
You might like
vBulletin HACK----显示话题大小和打开新窗口于论坛索引页
2006/10/09 PHP
php header()函数使用说明
2008/07/10 PHP
php简单的上传类分享
2016/05/15 PHP
CI框架(CodeIgniter)公共模型类定义与用法示例
2017/08/10 PHP
ThinkPHP5 的简单搭建和使用详解
2018/11/15 PHP
laravel框架中控制器的创建和使用方法分析
2019/11/23 PHP
PHP设计模式(九)外观模式Facade实例详解【结构型】
2020/05/02 PHP
基于jquery的鼠标拖动效果代码
2012/05/30 Javascript
基于dom编程中 动态创建与删除元素的使用
2013/04/17 Javascript
$.each遍历对象、数组的属性值并进行处理
2014/07/18 Javascript
window.location 对象所包含的属性
2014/10/10 Javascript
JS+CSS实现另类带提示效果的竖向导航菜单
2015/10/15 Javascript
Bootstrap Paginator分页插件与ajax相结合实现动态无刷新分页效果
2016/05/27 Javascript
require.js+vue开发微信上传图片组件
2016/10/27 Javascript
element ui table(表格)实现点击一行展开功能
2018/12/04 Javascript
微信小程序人脸识别功能代码实例
2019/05/07 Javascript
详解js根据百度地图提供经纬度计算两点距离
2019/05/13 Javascript
[02:38]DOTA2亚洲邀请赛 IG战队巡礼
2015/02/03 DOTA
Python时间戳使用和相互转换详解
2017/12/11 Python
python使用pycharm环境调用opencv库
2018/02/11 Python
PyCharm安装第三方库如Requests的图文教程
2018/05/18 Python
Python图像滤波处理操作示例【基于ImageFilter类】
2019/01/03 Python
python的移位操作实现详解
2019/08/21 Python
python颜色随机生成器的实例代码
2020/01/10 Python
Python中import导入不同目录的模块方法详解
2020/02/18 Python
python实现从ftp服务器下载文件
2020/03/03 Python
python-图片流传输的思路及示例(url转换二维码)
2020/12/21 Python
美国地毯购买网站:Rugs USA
2019/02/23 全球购物
我想声明一个指针并为它分配一些空间, 但却不行。这些代码有什么 问题?char *p; *p = malloc(10);
2016/10/06 面试题
应征英语教师求职信
2013/11/27 职场文书
工作疏忽检讨书
2014/01/25 职场文书
《台湾的蝴蝶谷》教学反思
2014/02/20 职场文书
2016大学军训心得体会
2016/01/11 职场文书
化工生产实习心得体会
2016/01/22 职场文书
在HTML5 localStorage中存储对象的示例代码
2021/04/21 Javascript
python数字图像处理之图像自动阈值分割示例
2022/06/28 Python