一,router 中的 index.js
//引入路由模块
import Vue from 'vue'
import VueRouter from 'vue-router'
import Film from '@/views/Film'
import Cinema from '@/views/Cinema'
import Center from '@/views/Center'
import NotFound from '@/views/NotFound'
import Comingsoon from '@/views/film/Comingsoon'
import Nowplaying from '@/views/film/Nowplaying'
Vue.use(VueRouter)// 注册路由模块
// 路由配置信息
const routes = [
{
path: '/film',
component: Film,
children: [
{
path: 'nowplaying',
component: Nowplaying
},
{
path: 'comingsoon',
component: Comingsoon
},
{
path: '/film',
redirect: '/film/nowplaying'
}
]
},
{
path: '/cinema',
component: Cinema
},
{
path: '/center',
component: Center
},
{
path: '*',
redirect: NotFound
}
]
const router = new VueRouter({
routes
})
export default router
二,路由守卫
全局守卫
router.beforeEach((to, from, next) => {
if (to.fullPath === '/center') {
if (localStorage.getItem('token')) {
next()
} else {
next('/login')
}
} else {
next()
}
})
组件内的守卫
beforeRouterEnter(to,from ,next){
console.log('组件内的守卫')
if(localStorage.getItem('token'){
next()
}else{
next('/login')
})
}