Vue 动态路由的实现(后台传递路由,前端拿到并生成侧边栏)

本文介绍如何实现Vue动态路由,详细阐述了从后端获取扁平化的路由数据,然后在前端转换并生成侧边栏的过程。涉及到的关键步骤包括调用接口获取路由数据、数据处理以及在login.vue组件中的应用。
摘要由CSDN通过智能技术生成

前言

后台传来当前用户对应权限的路由表(扁平数据),前端通过调接口拿到后处理(后端处理路由)

代码

后端扁平数据

[
  {id:1, pId:0, name:'example1', label:'菜单一', path:'/example1', component:'Main', icon:null},
  {id:2, pId:0, name:'example2', label:'菜单2', path:'/example2', component:'Main', icon:null},
  {id:3, pId:1, name:'table', label:'表格', path:'table', component:'/basic/table/index', icon:null},
  {id:4, pId:2, name:'tree', label:'树形菜单', path:'tree', component:'/basic/tree/index', icon:null}
]

路由

[
	{
	  name: 'example1',
	  path: '/example1',
	  component: Main,
	  meta: {
	    name : '菜单一'
	  },
	  children: [
	    {
	      name: 'table',
	      path: '/table',
	      component: () => import('_v//basic/table/index.vue'),
	      meta: {
	        name : '表格'
	      }
	    }
	  ]
	},
	 {
	   name: 'example2',
	   path: '/example2',
	   component: Main,
	   meta: {
	     name : '菜单2'
	   },
	   children: [
	     {
	       name: 'tree',
	       path: 'tree',
	       component: () => import('_v/basic/tree/index.vue'),
	       meta: {
	         name : '树形菜单'
	       }
	     }
	   ]
	 }
]

扁平数据转路由

const createRouter = () => new VueRouter({
   
  mode: 'hash',
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值