执行不到第一个为真时的return
if (!["/404", "/login", "/"].includes("/")) {
return "/";
} else {
return "/111";
}
知道了
if (["/404", "/login", "/"].includes(item.path)) {
return item.path;
} else {
return "/" + item.path;
}
/* -------------------------------- 导航守卫 -------------------------------- */
import router from './index';
import store from '@/store'
const routes = router.options.routes;
console.log(routes);
// 扁平化路由数据
var flattenData = [];
function flattening(menu) {
menu.map(item => {
// console.log(item);
if (item.children) {
flattening(item.children) // 递归执行
}
flattenData.push({
path: item.path,
// visible: item.visible,
meta: {
title: item.title,
id: item.id
}
})
});
}
flattening(routes);
// 将所有路径集合起来,用于前置守卫中判断
var paths = flattenData.map(item => {
console.log(["/404", "/login", "/"].includes(item.path));
if (["/404", "/login", "/"].includes(item.path)) {
return item.path;
} else {
return "/" + item.path;
}
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
console.log(to, from);
console.log(paths);
console.log(paths.includes(to.path));
/* if (!paths.includes(to.path)) {
next({ path: '/404' })
} */
if (sessionStorage.getItem("X-Token")) {
next()
} else {
if (to.path !== '/login') {
alert("未登录,请先登录!")
next({ path: '/login' })
} else {
next()
}
}
})
/* // 全局解析守卫
router.beforeResolve((to, from) => {
console.log(to,from)
}) */
// 全局后置钩子
router.afterEach((to, from) => {
// console.log(to, from)
sessionStorage.setItem("menu_id", to.meta.id);
store.commit("changeMenuid", to.meta.id)
})
自问自答 666
自己解决了,就记录一下