vue-router路由跳转判断用户是否存在

router.beforeEach((to, from, next) => {
		//console.log("to:", (to));
		//console.log("from:", (from));
		//console.log("next:", (next));
		if(to.path == '/login') {
			window.sessionStorage.removeItem('access-user');
			next();
		} else {
			let user = JSON.parse(window.sessionStorage.getItem('access-user'));
			var toUrl = to.path;
			console.log("jump to login. toUrl:", (toUrl));
			if(!user) {
				next({
					path: '/login',
					query: { url: toUrl }
				});
			} else {
				next();
			}
			next();
		}
	});

  

原文地址:https://www.cnblogs.com/luyuefeng/p/8378065.html