index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // 该文件专门用于创建整个应用的路由器
  2. import VueRouter from 'vue-router'
  3. //引入组件
  4. import About from '../pages/About'
  5. import Home from '../pages/Home'
  6. import News from '../pages/News'
  7. import Message from '../pages/Message'
  8. import Detail from '../pages/Detail'
  9. //创建并暴露一个路由器
  10. const router = new VueRouter({
  11. routes:[
  12. {
  13. name:'guanyu',
  14. path:'/about',
  15. component:About,
  16. meta:{isAuth:true,title:'关于'}
  17. },
  18. {
  19. name:'zhuye',
  20. path:'/home',
  21. component:Home,
  22. meta:{title:'主页'},
  23. children:[
  24. {
  25. name:'xinwen',
  26. path:'news',
  27. component:News,
  28. meta:{isAuth:true,title:'新闻'},
  29. /* beforeEnter: (to, from, next) => {
  30. console.log('前置路由守卫',to,from)
  31. if(to.meta.isAuth){ //判断是否需要鉴权
  32. if(localStorage.getItem('school')==='atguigu'){
  33. next()
  34. }else{
  35. alert('学校名不对,无权限查看!')
  36. }
  37. }else{
  38. next()
  39. }
  40. } */
  41. },
  42. {
  43. name:'xiaoxi',
  44. path:'message',
  45. component:Message,
  46. meta:{isAuth:true,title:'消息'},
  47. children:[
  48. {
  49. name:'xiangqing',
  50. path:'detail',
  51. component:Detail,
  52. meta:{isAuth:true,title:'详情'},
  53. //props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件。
  54. // props:{a:1,b:'hello'}
  55. //props的第二种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的所有params参数,以props的形式传给Detail组件。
  56. // props:true
  57. //props的第三种写法,值为函数
  58. props($route){
  59. return {
  60. id:$route.query.id,
  61. title:$route.query.title,
  62. a:1,
  63. b:'hello'
  64. }
  65. }
  66. }
  67. ]
  68. }
  69. ]
  70. }
  71. ]
  72. })
  73. //全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
  74. /* router.beforeEach((to,from,next)=>{
  75. console.log('前置路由守卫',to,from)
  76. if(to.meta.isAuth){ //判断是否需要鉴权
  77. if(localStorage.getItem('school')==='atguigu'){
  78. next()
  79. }else{
  80. alert('学校名不对,无权限查看!')
  81. }
  82. }else{
  83. next()
  84. }
  85. }) */
  86. //全局后置路由守卫————初始化的时候被调用、每次路由切换之后被调用
  87. router.afterEach((to,from)=>{
  88. console.log('后置路由守卫',to,from)
  89. document.title = to.meta.title || '硅谷系统'
  90. })
  91. export default router