vue学习笔记---路由拦截

//全局路由守卫 main.js
//刷新还是跳转,第一个进入的就是这个前置路由钩子函数
router.beforeEach((to,from,next) =>{
	//避免刷新vuex的token不见了
	store.commit('settoken',localStorage.getItem('token'))
	//如果自定义路由元信息;例如xxx:true代表需要验证token
	if(to.meta.xxxx){
		if(store.state.token){
			next()
		}else{
			//如果不存在token重定向到登录
			next({
				path:'/login',
				query:{redirect:to.fullPath}
			})
		}
	}else{
		next()
	}
})
发布了18 篇原创文章 · 获赞 3 · 访问量 1962

猜你喜欢

转载自blog.csdn.net/qq_42220283/article/details/104090350