vue 路由守卫 -- 全局前置守卫

384 阅读1分钟

beforeEach

在这个守卫里面可以做一些防止用户非法进入页面的操作,在进入页面的时候可以做一些用户信息验证,判断是否具有 token ,不具有的话禁止用户进入页面,可以提高安全性

router.beforeEach((to, from, next) => {
	const token = localStorage.token;
	if(token) {
		next();
	} else {
		if(to.path === '/index') {
			next()
		} else {
			next({
				path: '/index'
			})
		}
	}
})