vue中路由前置守卫

router.beforeEach((to, from, next) => {
    const name = sessionStorage.getItem("username");
    const token = sessionStorage.getItem("xb-token");
    console.log(to.name)
    if(to.name == 'PrescriptionHomepage'){
        $('.home-main').css({"background": "#000000"});
    }else{
        $('.home-main').css({"background": "#eef3f9"});
    }
    if (to.name === 'Login' || to.name === 'Register' || to.name === 'RegistrationAgreement' || to.name =='SignatureAgreement') {
        next();
    } else {
        if (token) {
            if (name) {
                next();
            } else {
                router.push("/xb-login")
            }
        } else {
            Message.warning('请先登录');
            router.push("/xb-login");
        }
    }
});

//to.name:获取路由跳转的name名称,此时路由跳转时,需要使用name进行调整。如下:
this.$router.push({
    name: "ChangePrescription",
    // query: {
    //     id: id,
    // }
 });
this.$router.push('/xb-home/xb-security/personalInformation');此方式获取不到.name属性值。
//如果获取的是to.path进行判断可以使用路径方式进行跳转。如下:
this.$router.push('/xb-home/xb-security/personalInformation');

 

你可能感兴趣的:(vue中路由前置守卫)