写管理后台的都会遇到这个这种需求,管理员登录后台需要看到所有功能,普通的公司管理员或部门管理员登录只能看到部分功能权限
不同角色有不同的权限,这时如果管理平台的菜单定义在代码里,显然是不能满足上面的需求,我们就需要后台提供用户菜单,然后前台动态生成路由。
1 动态添加路由
利用 vue-router 的 addRoutes 方法可以动态添加路由。请求接口返回菜单格式如下
[
{
"id":"1332194352530083842",
"pid":"1331859957655007233",
"name":"TspUser",
"component":"user/tspuser/index",
"redirect":null,
"meta":{
"title":"用户信息 ",
"icon":"pic-right",
"show":true,
"target":null,
"link":null
},
"path":"/user",
"hidden":false
},
{
"id":"1334396728339042306",