为了验证登陆和页面跳转,使用router.beforeEach,具体样例如下:
import router from './router'
router.beforeEach((to, from, next) => {
console.log(from);
console.log(to);
var userInfo = sessionStorage.getItem('user');//当做用户的登陆情况通过此步获取
if (userInfo) { //检查是否已登陆
if (to.matched.length !== 0) {//判断路径是否满足路由配置中的路径
if (to.path == '/login' || to.path == '/' || to.path == '') {//判断是否登陆连接,如果是就不让去登陆页面,返回上一步的页面
if (from.path == '/login' || from.path == '/' || from.path == '') {//判断如果从登陆页面去登陆页码,在已登录时,回到首页
next('/home')
} else {
next(from.fullPath)
}
} else {
next()
}
} else {
if (from.fullPath == '/') {
next('/login');
} else {
next(from.fullPath)
}
}
} else {//没有登陆时,如果不是login就跳到登陆页面,如果时登陆就前往
if (to.path == '/' || to.path == '') {
next('/login');
} else if (to.path == '/login') {
var v = store.state.to_path
if (v == '/login' || v == '/') {
store.commit('set_path', '/home')
}
next();
}
else {
if (to.matched.length !== 0) {
store.commit('set_path', to.path)
next('/login');
} else {
store.commit('set_path', '/home')
next('/login');
}
}
}
});
网友评论