全局前置守卫:首先需要注册全局路由
import router from "./router";
router.beforeEach((to, from, next) => {
// ...
});
new Vue({
el: "#app",
store,
router,
components: {
App },
template: "<App/>"
});
router.beforeEach((to, from, next) => {
if (to.matched.some(m => m.meta.auth)) {
// 对路由进行验证
if (isLogin("isLogin") === "1") {
// 根据后台返回值判断是否登录过期
// 已经登陆
next(); // 正常跳转页面
} else {
// 未登录则跳转到登陆界面,query:{
Rurl: to.fullPath}表示把当前路由信息传递过去方便登录后跳转回来;
next({
path: "/", query: {
} });
}
} else {
next();
}
});
路由独享的守卫:在路由配置上使用
routes: [
{
path: "/foo",
component: Foo,
befo