在Vue中如何使用Cookie操作实例

2020-06-12 21:22:53易采站长站整理

] }
];

const router = new Router({
routes
});

备注:请注意路由中的 meta:{requireAuth: true },这个配置,主要为下面的验证做服务。

if(to.meta.requireAuth),这段代码意思就是说,如果requireAuth: true ,那就判断用户是否存在。

如果存在,就继续执行下面的操作,如果不存在,就删除客户端的Cookie,同时页面跳转到登录页,代码如下。


//这个是请求页面路由的时候会验证token存不存在,不存在的话会到登录页
router.beforeEach((to, from, next) => {
if(to.meta.requireAuth) {
fetch('m/is/login').then(res => {
if(res.errCode == 200) {
next();
} else {
if(getCookie('session')) {
delCookie('session');
}
if(getCookie('u_uuid')) {
delCookie('u_uuid');
}
next({
path: '/'
});
}
});
} else {
next();
}
});
export default router;

以上就是Cookie在项目中的使用,希望对大家的学习有所帮助,也希望大家多多支持软件开发网。