在项目中有些页面仅能由有相应权限的用户才能查看,这时候可以给路由设置路由验证,实现权限控制功能。代码如下:
let routes
=[{
path
:"/a/:id",
component
:myA
,
meta
:{
roles
:['administrator','customer']
}
},{
path
:"/b",
component
:myB
,
meta
:{
roles
:['administrator']
}
}];
let router
=new VueRouter({
routes
:routes
});
const userRole
='administrator'
router
.beforeEach((to
,from,next
)=>{
if(to
.meta
.roles
.includes(userRole
)){
next();
}else{
}
});
转载请注明原文地址: https://lol.8miu.com/read-10776.html