Commit 41bb7de6 authored by 陈晓阳's avatar 陈晓阳

新增 路由守卫

parent d4b4c40d
...@@ -12,3 +12,19 @@ new Vue({ ...@@ -12,3 +12,19 @@ new Vue({
router, router,
render: h => h(App) render: h => h(App)
}); });
router.beforeEach(function(to, from, next) {
console.log(to);
if (to.meta.auth) {
const token = localStorage.getItem("token");
if (token) {
// 要访问后台主页,需要判断是否有 token
next();
} else {
// 没有登录,强制跳转到登录页
next({ path: "/" });
}
} else {
next();
}
});
...@@ -11,17 +11,26 @@ export default new Router({ ...@@ -11,17 +11,26 @@ export default new Router({
{ {
path: "/", path: "/",
name: "Welcome", name: "Welcome",
component: Welcome component: Welcome,
meta: {
auth: false
}
}, },
{ {
path: "/register", path: "/register",
name: "Register", name: "Register",
component: Register component: Register,
meta: {
auth: true
}
}, },
{ {
path: "/success", path: "/success",
name: "Success", name: "Success",
component: Success component: Success,
meta: {
auth: true
}
} }
] ]
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment