优化登录跳转规则
This commit is contained in:
+23
-5
@@ -1,9 +1,10 @@
|
||||
import Vue from 'vue'
|
||||
import VueRouter from 'vue-router'
|
||||
import store from './store'
|
||||
|
||||
import LoginPage from './pages/login/LoginPage.vue'
|
||||
import RegisterPage from './pages/login/RegisterPage.vue'
|
||||
import IndexPage from './pages/index/IndexPage.vue'
|
||||
import ChatPage from './pages/index/ChatPage.vue'
|
||||
|
||||
Vue.use(VueRouter)
|
||||
|
||||
@@ -23,16 +24,33 @@ const routes = [
|
||||
component: RegisterPage
|
||||
},
|
||||
{
|
||||
path: '/index',
|
||||
name: 'index',
|
||||
component: IndexPage
|
||||
path: '/chat',
|
||||
name: 'chat',
|
||||
component: ChatPage,
|
||||
meta: {
|
||||
requiresAuth: true
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
const router = new VueRouter({
|
||||
mode: 'history',
|
||||
mode: 'history',
|
||||
base: process.env.BASE_URL,
|
||||
routes
|
||||
})
|
||||
|
||||
// 路由守卫,当访问需要登录的页面时,检查是否有token,如果没有则跳转到登录页
|
||||
router.beforeEach((to, from, next) => {
|
||||
const token = store.state.token;
|
||||
if(to.meta.requiresAuth) {
|
||||
if(token) {
|
||||
next()
|
||||
} else {
|
||||
next('/login')
|
||||
}
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user