直接上代碼:
首先在你的登錄頁(yè)面需要存儲(chǔ)一個(gè)localStorage數(shù)據(jù),
登錄成功
localStorage.setItem("islogin", JSON.stringify(this.formInline));
然后在vue的全局js中找到main.js文件添加路由守衛(wèi)
router.beforeEach((to, from, next) => {
if (to.meta.requireAuth) {
if (JSON.parse(localStorage.getItem("islogin"))) {
next();
} else {
next({
path: "/SignIn"http://指向?yàn)槟愕牡卿浗缑? });
}
} else {
next();
}
if (to.fullPath === "/SignIn") {
if (JSON.parse(localStorage.getItem("islogin"))) {
next({
path: from.fullPath
});
} else {
next();
}
}
});
最后一步棉钧,在router文件找到你全局路由設(shè)置在每一個(gè)界面里面加入路由守衛(wèi)
{
name: "Portal",
path: "/Portal",
// 路由守衛(wèi)
meta: { requireAuth: true },
props: true,
component: () => import("@/components/modules/Portal")
},
meta: { requireAuth: true },將開(kāi)啟你的頁(yè)面路由守衛(wèi)