import Vue from 'vue'
import VueRouter from 'vue-router'
// import Film from '../views/Film.vue'
Vue.use(VueRouter)
const routes = [
// 重定向
{
path: '*',
redirect: '/film'
},
{
path: '/film',
component: () => import('@/views/Film.vue'),//路由懒加载
// 嵌套路由
children: [
{
path: 'nowplaying',
component: () => import('@/views/film/Nowplaying.vue')
},
]
},
// {
// path:'/detail/:myid',//动态路由
// component:Detail,
// name:'kerwinDetail'
// },
{
path: '/detail',
component: () => import('@/views/Detail.vue'),
},
{
path: '/center',
component: () => import('@/views/Center.vue')
},
]
const router = new VueRouter({
//路由模式
// a.hash #/home
// b.history /home
mode: 'hash',
routes
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
const auth = ['/center', '/order']
if (auth.includes(to.fullPath)) {
// 拦截
if (!localStorage.getItem('token')) {
next('/login')
} else {
// 放行
next()
}
} else {
next()
}
})
export default router
// 组件内的路由拦截
beforeRouteEnter(to,from,next){
// 拦截
if(!localStorage.getItem('token')){
next('/login')
}else{
next()
}
},
路由跳转
跳转携带参数
handleClick(id){
// 1.路径
// this.$router.push(`/detail/${id}`)
// 2.路由名字
// this.$router.push({
// name:'kerwinDetail',
// params:{
// myid:id
// }
// })
// 3.query方式跳转详情
this.$router.push(`/detail?id=${id}`)
},
目标页获得参数
// 1,2
console.log(this.$route.params.myid);
// 3
console.log(this.$route.query.id);
路由配置
//1,
{
path:'/detail/:myid',//动态路由
component:Detail,
name:'kerwinDetail'
},
//2,3
{
path: '/detail',
component: () => import('@/views/Detail.vue'),
},
项目源码:
链接:https://pan.baidu.com/s/1K8HnVEZy87O6-gl6qsZnxw
提取码:t70u
复制这段内容后打开百度网盘手机App,操作更方便哦