2022-08-16 19:57:31 +08:00

30 lines
781 B
JavaScript
Vendored
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Vue from "vue";
import VueRouter from "vue-router";
// import axios from "axios";
Vue.use(VueRouter);
const createRouter = () =>
new VueRouter({
scrollBehavior: () => ({ y: 0 }),
});
const router = createRouter();
router.beforeEach((to, from, next) => {
// console.log(to,next);
const token = localStorage.getItem("token");
// 目标路由不是登录页并且还需要token验证还没有token那就直接给返回到登录页
if (to.name !== "Login" && !token) {
next({ name: "Login" });
} else {
// 目标路由是登录页-自然不需要token验证
// 或目标路由不需要身份验证
// 又或目标路由非登录页需要token验证但是有token
// next放行
next();
}
});
export default router;