直接上代碼:
首先在你的登錄頁面需要存儲一個localStorage數據,
登錄成功
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://指向為你的登錄界面
});
}
} else {
next();
}
if (to.fullPath === "/SignIn") {
if (JSON.parse(localStorage.getItem("islogin"))) {
next({
path: from.fullPath
});
} else {
next();
}
}
});
最后一步,在router文件找到你全局路由設置在每一個界面里面加入路由守衛(wèi)
{
name: "Portal",
path: "/Portal",
// 路由守衛(wèi)
meta: { requireAuth: true },
props: true,
component: () => import("@/components/modules/Portal")
},
meta: { requireAuth: true },將開啟你的頁面路由守衛(wèi)