目录
路由导航守卫
代码语言:javascript
复制import Vue from 'vue'
import VueRouter from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
Vue.use(VueRouter)
const routes=
[
{
path: '/',
name: 'HelloWorld',
redirect: '/login',
component: HelloWorld
},
{
path: '/login',
name: 'Login',
component: Login
},
{
path: '/home',
name: 'home',
component: HelloWorld
},
]
const router =new VueRouter({
routes
})
// 路由导航守卫
router.beforeEach((to,form,next)=>{
if(to.path == '/login'){
next()
}else{
const token = window.sessionStorage.getItem('token');
if(!token){
next('/login')
}else{
next()
}
}
})
export default router