• 路由router


    1. 什么是路由?

      1. 一个路由就是一组映射关系(key - value)
      2. key 为路径,value 可能是 function 或 component

    2、安装\引入\基础使用
    只有vue-router3,才能应用于vue2;vue-router4可以应用于vue3中

    这里我们安装vue-router3:npm i vue-router@3

    引入vue-router:在入口js中引入:import VueRouter from 'vue-router'

    vue.use(VueRouter)

     

    多级路由

    即是由多个路由相互嵌套而形成的

    Banner作为title直接在App.vue中实现

    然后是About和Home作为路由组件在App.vue中。

    message和news继而继续嵌套在home中

    main.js

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. //引入VueRouter
    4. import VueRouter from 'vue-router'
    5. //引入路由器
    6. import router from './router'
    7. Vue.config.productionTip = false
    8. //应用插件
    9. Vue.use(VueRouter)
    10. new Vue({
    11. el:"#app",
    12. render: h => h(App),
    13. router
    14. })

    App.vue

    1. <template>
    2. <div>
    3. <div class="row">
    4. <Banner/>
    5. </div>
    6. <div class="row">
    7. <div class="col-xs-2 col-xs-offset-2">
    8. <div class="list-group">
    9. <!-- 原始html中我们使用a标签实现页面跳转 -->
    10. <!-- <a class="list-group-item active" href="./about.html">About</a>
    11. <a class="list-group-item" href="./home.html">Home</a> -->
    12. <!-- Vue中借助router-link标签实现路由的切换 -->
    13. <router-link class="list-group-item" active-class="active" to="/about">
    14. About
    15. </router-link>
    16. <router-link class="list-group-item" active-class="active" to="/home">
    17. Home
    18. </router-link>
    19. </div>
    20. </div>
    21. <div class="col-xs-6">
    22. <div class="panel">
    23. <div class="panel-body">
    24. <!-- 指定组件的呈现位置 -->
    25. <router-view></router-view>
    26. </div>
    27. </div>
    28. </div>
    29. </div>
    30. </div>
    31. </template>
    32. <script>
    33. import Banner from './components/Banner.vue'
    34. export default {
    35. name:'App',
    36. components:{Banner,}
    37. }
    38. </script>

    router/index.js

    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. const router = new VueRouter({
    9. routes:[
    10. {
    11. path:'/about',
    12. component:About
    13. },
    14. {
    15. path:'/home',
    16. component:Home,
    17. children:[
    18. {
    19. path:'news',
    20. component:News,
    21. },
    22. {
    23. path:'message',
    24. component:Message,
    25. }
    26. ]
    27. }
    28. ]
    29. })
    30. export default router

    Banner.vue

    1. <template>
    2. <div class="col-xs-offset-2 col-xs-8">
    3. <div class="page-header"><h2>Vue Router Demo</h2></div>
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name:'Banner'
    9. }
    10. </script>
    11. <style>
    12. </style>

    About.vue

    1. <template>
    2. <h2>我是About的内容</h2>
    3. </template>
    4. <script>
    5. export default {
    6. name:'About'
    7. }
    8. </script>

    Home

    1. <template>
    2. <div>
    3. <h2>Home组件内容</h2>
    4. <div>
    5. <ul class="nav nav-tabs">
    6. <li>
    7. <router-link class="list-group-item" active-class="active" to="/home/news">News</router-link>
    8. </li>
    9. <li>
    10. <router-link class="list-group-item " active-class="active" to="/home/message">Message</router-link>
    11. </li>
    12. </ul>
    13. <ul>
    14. <router-view></router-view>
    15. </ul>
    16. </div>
    17. </div>
    18. </template>
    19. <script>
    20. export default {
    21. name:'Home'
    22. }
    23. </script>

    message

    1. <template>
    2. <div>
    3. <div>
    4. <ul>
    5. <li>
    6. <a href="/message1">message001</a>&nbsp;&nbsp;
    7. </li>
    8. <li>
    9. <a href="/message2">message002</a>&nbsp;&nbsp;
    10. </li>
    11. <li>
    12. <a href="/message/3">message003</a>&nbsp;&nbsp;
    13. </li>
    14. </ul>
    15. </div>
    16. </div>
    17. </template>
    18. <script>
    19. export default {
    20. name:'Mesage',
    21. }
    22. </script>
    23. <style>
    24. </style>

    news

    1. <template>
    2. <ul>
    3. <li>news001</li>
    4. <li>news002</li>
    5. <li>news003</li>
    6. </ul>
    7. </template>
    8. <script>
    9. export default {
    10. name:'News',
    11. }
    12. </script>
    13. <style>
    14. </style>

    query的传参

    若是有很多的嵌套的情况下,一直如上嵌套是不现实的,所以可以通过传参的方法,将需要传递的参数直接带到下一个页面中

    下例即是在message下继续嵌套

    index.js(引入继续嵌套的detail)

    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. const router = new VueRouter({
    10. routes:[
    11. {
    12. path:'/about',
    13. component:About
    14. },
    15. {
    16. path:'/home',
    17. component:Home,
    18. children:[
    19. {
    20. path:'news',
    21. component:News,
    22. },
    23. {
    24. path:'message',
    25. component:Message,
    26. children:[
    27. {
    28. path:'detail',
    29. component:Detail,
    30. }
    31. ]
    32. }
    33. ]
    34. }
    35. ]
    36. })
    37. export default router

    message(传递query参数)

    1. <template>
    2. <div>
    3. <div>
    4. <ul>
    5. <li v-for="m in messageList" :key="m.id">
    6. <!-- 跳转路由并携带query参数,to的字符串写法 -->
    7. <!-- <router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>&nbsp;&nbsp; -->
    8. <!-- 跳转路由并携带query参数,to的对象写法 -->
    9. <router-link :to="{
    10. path:'/home/message/detail',
    11. query:{
    12. id:m.id,
    13. title:m.title
    14. }
    15. }">
    16. {{m.title}}
    17. </router-link>
    18. </li>
    19. </ul>
    20. <hr>
    21. <router-view></router-view>
    22. </div>
    23. </div>
    24. </template>
    25. <script>
    26. export default {
    27. name:'Mesage',
    28. data(){
    29. return{
    30. messageList:[
    31. {id:'001',title:'消息001'},
    32. {id:'002',title:'消息002'},
    33. {id:'003',title:'消息003'},
    34. ]
    35. }
    36. }
    37. }
    38. </script>
    39. <style>
    40. </style>

    detail(接收参数)

    1. <template>
    2. <ul>
    3. <li>消息编号:{{$route.query.id}}</li>
    4. <li>消息标题:{{$route.query.title}}</li>
    5. </ul>
    6. </template>
    7. <script>
    8. export default {
    9. name:'Detail',
    10. mounted(){
    11. console.log(this.$route)
    12. }
    13. }
    14. </script>
    15. <style>
    16. </style>

    replace 

    类似于无痕浏览,即当前的router-link标签若加上了这个,则当前对该标签的操作是不可追回的

    App。vue

    1. <template>
    2. <div>
    3. <div class="row">
    4. <Banner/>
    5. </div>
    6. <div class="row">
    7. <div class="col-xs-2 col-xs-offset-2">
    8. <div class="list-group">
    9. <!-- 原始html中我们使用a标签实现页面跳转 -->
    10. <!-- <a class="list-group-item active" href="./about.html">About</a>
    11. <a class="list-group-item" href="./home.html">Home</a> -->
    12. <!-- Vue中借助router-link标签实现路由的切换 -->
    13. <router-link replace class="list-group-item" active-class="active" to="/about">
    14. About
    15. </router-link>
    16. <router-link replace class="list-group-item" active-class="active" to="/home">
    17. Home
    18. </router-link>
    19. </div>
    20. </div>
    21. <div class="col-xs-6">
    22. <div class="panel">
    23. <div class="panel-body">
    24. <!-- 指定组件的呈现位置 -->
    25. <router-view></router-view>
    26. </div>
    27. </div>
    28. </div>
    29. </div>
    30. </div>
    31. </template>
    32. <script>
    33. import Banner from './components/Banner.vue'
    34. export default {
    35. name:'App',
    36. components:{Banner,}
    37. }
    38. </script>

    activated和deactivated

    这是路由组件中两个独有的生命周期钩子,用于捕获路由组件的生命周期状态

    1. 具体使用:
      1. activated路由组件被激活时触发
      2. deactivated路由组件失活时触发

    News

    1. <template>
    2. <ul>
    3. <li>news001 <input type="text"></li>
    4. <li>news002 <input type="text"></li>
    5. <li>news003 <input type="text"></li>
    6. </ul>
    7. </template>
    8. <script>
    9. export default {
    10. name:'News',
    11. data(){
    12. return{
    13. opacity:1
    14. }
    15. },
    16. activated(){
    17. console.log('News组件被激活了')
    18. },
    19. deactivated(){
    20. console.log('News组件失活了')
    21. }
    22. }
    23. </script>

    路由守卫

    例子:在淘宝中,若是不经过登录,则是无法跳转到个人中心页面,即使点击个人中心,也是不能的,这就是路由守卫

    前置首位

    index.js

    1. //该文件专门用于创建整个应用的路由器
    2. import VueRouter from "vue-router";
    3. //引入组件
    4. import Home from '../pages/Home'
    5. import About from '../pages/About'
    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. },
    17. {
    18. name:'zhuye',
    19. path:'/home',
    20. component:Home,
    21. children:[
    22. {
    23. name:'xinwen',
    24. path:'news',
    25. component:News,
    26. meta:{isAuth:true}
    27. },
    28. {
    29. name:'xiaoxi',
    30. path:'message',
    31. component:Message,
    32. meta:{isAuth:true},
    33. children:[
    34. {
    35. name:'xiangqing',
    36. path:'detail',
    37. component:Detail,
    38. props($route){
    39. return {
    40. id:$route.query.id,
    41. title:$route.query.title,
    42. }
    43. }
    44. }
    45. ]
    46. }
    47. ]
    48. }
    49. ]
    50. })
    51. //全局前置路由守卫--初始化时被调用,每次路由切换之前被调用
    52. router.beforeEach((to,from,next)=>{
    53. console.log(to,from)
    54. if(to.meta.isAuth){ //判断是否需要鉴权
    55. if(localStorage.getItem('school')==='atguigu'){
    56. next()
    57. }
    58. else{
    59. alert('学校名不对,无权限查看')
    60. }
    61. }
    62. else{
    63. next()
    64. }
    65. })
    66. export default router

    全局守卫

    对路由组件权限的控制

    index.js

    1. //该文件专门用于创建整个应用的路由器
    2. import VueRouter from "vue-router";
    3. //引入组件
    4. import Home from '../pages/Home'
    5. import About from '../pages/About'
    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:{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. },
    30. {
    31. name:'xiaoxi',
    32. path:'message',
    33. component:Message,
    34. meta:{isAuth:true,title:'消息'},
    35. children:[
    36. {
    37. name:'xiangqing',
    38. path:'detail',
    39. component:Detail,
    40. meta:{isAuth:true,title:'详情'},
    41. props($route){
    42. return {
    43. id:$route.query.id,
    44. title:$route.query.title,
    45. }
    46. }
    47. }
    48. ]
    49. }
    50. ]
    51. }
    52. ]
    53. })
    54. //全局前置路由守卫--初始化时被调用,每次路由切换之前被调用
    55. router.beforeEach((to,from,next)=>{
    56. console.log('前置路由守卫',to,from)
    57. if(to.meta.isAuth){ //判断是否需要鉴权
    58. if(localStorage.getItem('school')==='atguigu'){
    59. next()
    60. }
    61. else{
    62. alert('学校名不对,无权限查看')
    63. }
    64. }
    65. else{
    66. next()
    67. }
    68. })
    69. //全局后置路由守卫--初始化时被调用,每次路由切换之后被调用
    70. router.afterEach((to,from)=>{
    71. console.log('后置路由守卫',to,from)
    72. document.title=to.meta.title
    73. })
    74. export default router

    独享路由守卫,针对于特别需求坐单独的路由守卫

    index.js

    1. //该文件专门用于创建整个应用的路由器
    2. import VueRouter from "vue-router";
    3. //引入组件
    4. import Home from '../pages/Home'
    5. import About from '../pages/About'
    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:{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. }
    35. else{
    36. alert('学校名不对,无权限查看')
    37. }
    38. }
    39. else{
    40. next()
    41. }
    42. }
    43. },
    44. {
    45. name:'xiaoxi',
    46. path:'message',
    47. component:Message,
    48. meta:{isAuth:true,title:'消息'},
    49. children:[
    50. {
    51. name:'xiangqing',
    52. path:'detail',
    53. component:Detail,
    54. meta:{isAuth:true,title:'详情'},
    55. props($route){
    56. return {
    57. id:$route.query.id,
    58. title:$route.query.title,
    59. }
    60. }
    61. }
    62. ]
    63. }
    64. ]
    65. }
    66. ]
    67. })
    68. // //全局前置路由守卫--初始化时被调用,每次路由切换之前被调用
    69. // router.beforeEach((to,from,next)=>{
    70. // console.log('前置路由守卫',to,from)
    71. // if(to.meta.isAuth){ //判断是否需要鉴权
    72. // if(localStorage.getItem('school')==='atguigu'){
    73. // next()
    74. // }
    75. // else{
    76. // alert('学校名不对,无权限查看')
    77. // }
    78. // }
    79. // else{
    80. // next()
    81. // }
    82. // })
    83. // //全局后置路由守卫--初始化时被调用,每次路由切换之后被调用
    84. // router.afterEach((to,from)=>{
    85. // console.log('后置路由守卫',to,from)
    86. // document.title=to.meta.title
    87. // })
    88. export default router

  • 相关阅读:
    SpringBoot日志输出
    Nginx源码解析--configure
    1015: 【C1】【循环】【for】整数序列的元素最大跨度值
    【Docker 内核详解】cgroups 资源限制(一):概念、作用、术语
    线性回归法学习笔记
    SpringBoot整合MinIO
    B站崩了,那晚负责修复的开发人员做了什么?
    【送书活动1】强势挑战Java,Kotlin杀回TIOBE榜单Top 20!
    【WSN布局】基于LICHTENBERG算的多目标传感器选择和放置优化问题研究附matlab代码
    了解CI/CD流水线
  • 原文地址:https://blog.csdn.net/2301_76386566/article/details/133786390