const noMenu = ["/login"];
const router = useRouter();
const state = reactive({
defaultOpen: ["1", "2", "3", "4"],
showMenu: true,
currentPath: "/dashboard",
count: {
number: 1
}
});
// 监听浏览器原生回退事件
if (window.history && window.history.pushState) {
history.pushState(null, null, document.URL);
window.addEventListener(
"popstate",
() => {
if (!localGet("token")) {
state.showMenu = false;
}
},
false
);
}
const unwatch = router.beforeEach((to, from, next) => {
if (to.path == "/login") {
// 如果路径是 /login 则正常执行
next();
} else {
// 如果不是 /login,判断是否有 token
if (!localGet("token")) {
// 如果没有,则跳至登录页面
next({ path: "/login" });
} else {
// 否则继续执行
next();
}
}
state.showMenu = !noMenu.includes(to.path);
state.currentPath = to.path;
document.title = pathMap[to.name];