vue.js 拦截器

document.cookie = "mylogin=1";//1:登陆成功;保存登录状态

main.js

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    var login = 0;
     console.log("--------------document-----------",document.cookie);
    var ck = document.cookie.split(";");
    
    for (var index = 0; index < ck.length; index++) {
        var islogin = ck[index].split("=");
        if (islogin[0].trim() == "mylogin") {
            if (islogin[1] == 1) { //还在登录状态
                login = 1;
                break;
            }
        }
    }
    console.log("--------------login-----------",login);
    if (login == 1) { //还在登录状态
        next();
    } else {
        next({
            path: '/',
            name: 'login',
            query: { redirect: to.fullPath }
        })
    }

    } else {
    next();
    }
})
posted @ 2017-08-07 13:35  次序  阅读(2365)  评论(0编辑  收藏  举报