代码如下
import { createRouter, createWebHistory, useRouter } from "vue-router";
// 引入路由表配置
import routes from "./routes";
// 引入接口
import { user_routers } from "../network/user";
const modules = import.meta.glob("../views/**/*.vue");
console.log(modules);
const router = createRouter({
history: createWebHistory(),
routes,
scrollBehavior(to, from, savedPosition) {
//滚动行为
// return 期望滚动到哪个的位置
if (savedPosition) {
return savedPosition;
} else {
return { left: 0, top: 0 };
}
},
});
// 路由前置守卫
//白名单
let whiteList = ["/login", "/not-found"];
let menu: any;
router.beforeEach(async (to, from) => {
console.log(to.path);
let token = localStorage.getItem("_token");
//未登录
if (!token) {
//检测白名单
if (whiteList.indexOf(to.path) == -1) {
// 不在白名单,则去login
return { path: "/login" };
}
} else {