基于vue的商城网站需要在用户购买商品时进行判断是否登录
在app.vue中的created钩子中去监听路由需要判断是否要登录的页面
router.beforeEach((to, from, next) => {
if(!getStorage("access_token")){
//跳到登录页面 设定哪些页面 暂定购买
if(to.path.indexOf('buy') > -1 || to.path.indexOf('cart') > -1){
setStorage("toPath",to.path);
// window.location.href = "/login";
window.open ("/login",'_black');//跳转到登录打开新页面
}else{
removeStorage("toPath");
this.getUserInfo();
this.checkProdStateAndDispatch(to);//触发action
next();
}
}else{
removeStorage("toPath");
this.getUserInfo();
this.checkProdStateAndDispatch(to);//触发action
next();
}
});