用户操作跳转页面时,判断是否已登录,登录则跳转到指定页面,反之,跳转到登录页面重新登录
router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import City from '@/components/city'
import Login from '@/components/login'
import Chat from '@/components/chat.vue'
import Vuex from 'vuex'
Vue.use(Vuex)
Vue.use(Router)
const router = new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/city',
name: 'city',
component: City
},
{
path: '/login',
name: 'login',
component: Login
},
{
path: '/chat',
name: 'chat',
component: Chat
}
]
});
router.beforeEach((to, from, next) => {
if (to.path === '/login') {
next();
} else {
let token = sessionStorage.getItem('Author');
if (!token) {
next('/login');
} else {
next();
}
}
});
export default router;