Vue路由中resetRouter重置routes的参数hidden时无法生效
记录:如题,当在静态的router中设置伪权限,即设置routes路由的参数hidden
代码如下:
import store from "@/store";
// 伪权限,设置定时器的原因是router/index.js中无法及时拿到store
var timer = setInterval(() =>{
if (store && store.getters.admin_id && process.env.VUE_APP_ADMIN_IDS) {
clearInterval(timer);
const flag = JSON.parse(process.env.VUE_APP_ADMIN_IDS).includes(store.getters.admin_id);
constantRoutes.map(item => {
if (item.path === '/cost') {
// 注意:这里设置hidden参数,必须保障原来routes中有hidden属性,默认设置 hidden: false;
item.hidden = !flag;
}
})
resetRouter()
}
}, 100);
function resetRouter(){
const newRouter = createRouter();
router.matcher = newRouter.matcher;
}
注意:动态设置routes中路由属性是必须保证routes中有对应的属性和默认值,确保数据双向绑定