在網上嘗試過很多方法都不可以,包括重置路由,刷新頁面的時候可以,但是使用起來對於用戶不太友好,后來經過努力查找,發現經過深拷貝的OK的,以此記錄
文件路徑 src/store/modules/permission.js
// 從lodash中引入深拷貝
import cloneDeep from "lodash.clonedeep";
//修改此函數
function filterAsyncRouter(routerMap, roles) {
// 增加此行是因為如果不深拷貝,會更改原路由表,當切換用戶時,會出現用戶該有的菜單無法顯示
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;
}
