文件路徑 src/store/modules/permission.js
// 從lodash中引入深拷貝
import cloneDeep from "lodash.clonedeep";
//修改此函數(shù)
function filterAsyncRouter(routerMap, roles) {
// 增加此行是因為如果不深拷貝,會更改原路由表,當(dāng)切換用戶時,會出現(xiàn)用戶該有的菜單無法顯示
let asyncRouterMap = cloneDeep(routerMap);
const accessedRouters = asyncRouterMap.filter(route => {
if (hasPermission(roles.permissionList, route)) {
if (route.children && route.children.length) {
route.children = filterAsyncRouter(route.children, roles);
}
return true;
}
return false;
});
return accessedRouters;
}