You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import Vue from 'vue'
import App from './App.vue'
import './plugins/axios'
import router from './router'
import store from './store'
import ElementUI from 'element-ui' ;
import 'element-ui/lib/theme-chalk/index.css' ;
import Cookie from "js-cookie" ;
Vue . config . productionTip = false
Vue . use ( ElementUI ) ;
//添加全局前置导航守卫
router . beforeEach ( ( to , from , next ) => {
//判断token是否存在
const token = Cookie . get ( 'token' )
//token不存在且当前不在登录或注册页面,说明用户未登录,跳转至登录页面
if ( ! token && to . name === 'register' ) {
next ( )
}
else if ( ! token && to . name === 'login' ) {
next ( )
}
//token不存在且当前为游客模式, 放行
else if ( ! token && to . name !== 'visitor' ) {
next ( { name : 'visitor' } )
}
else if ( token && to . name === 'login' ) { //token存在且当前页面为登录页面, 用户已登录, 跳转至首页
next ( { name : 'home' } )
}
else {
next ( )
}
} )
new Vue ( {
router ,
store ,
el : '#app' ,
render : h => h ( App )
} ) . $mount ( '#app' )