VueRouter路由嵌套

怼烎@ 2024-03-30 18:02 212阅读 0赞

1. 前言

本小节我们介绍如何嵌套使用 VueRouter。嵌套路由在日常的开发中非常常见,如何定义和使用嵌套路由是本节的重点。同学们在学完本节课程之后需要自己多尝试配置路由。

2. 配置嵌套路由

实际项目中的应用界面,通常由多层嵌套的组件组合而成。同样地,URL 中各段动态路径也按某种结构对应嵌套的各层组件,例如:

  1. /article/vue /article/react
  2. +------------------+ +-----------------+
  3. | Article | | Article |
  4. | +--------------+ | | +-------------+ |
  5. | | Vue | | +------------> | | React | |
  6. | | | | | | | |
  7. | +--------------+ | | +-------------+ |
  8. +------------------+ +-----------------+

借助 vue-router,使用嵌套路由配置,就可以很简单地表达这种关系。
在上一小节中我们学习了如何配置一个路由信息:

  1. {
  2. path: '路由地址',
  3. component: '渲染组件'
  4. }

要配置嵌套路由,我们需要在配置的参数中使用 children 属性:

  1. {
  2. path: '路由地址',
  3. component: '渲染组件',
  4. children: [
  5. {
  6. path: '路由地址',
  7. component: '渲染组件'
  8. }
  9. ]
  10. }

2.1 基本使用

接下来我们对上一小节的例子来做一个改造:在文章页面,我们对文章进行分类,提供两个链接按钮 vue、react,点击可以跳转到对应的文章列表,具体代码示例如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <div>
  12. <router-link to="/index">首页</router-link>
  13. <router-link to="/article">文章</router-link>
  14. </div>
  15. <router-view></router-view>
  16. </div>
  17. </body>
  18. <script src="https://unpkg.com/vue/dist/vue.js"></script>
  19. <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  20. <script type="text/javascript">const Index = Vue.component('index', {
  21. template: '<div>Hello,欢迎使用编程笔记学习 Vue 教程!</div>',
  22. })
  23. const Article = Vue.component('myArticle', {
  24. template: `<div>
  25. <div>
  26. <router-link to="/article/vue">vue</router-link>
  27. <router-link to="/article/react">react</router-link>
  28. </div>
  29. <router-view></router-view>
  30. </div>`,
  31. })
  32. const VueArticle = Vue.component('vueArticle', {
  33. template: `<ul><li>1. Vue 基础学习</li><li>2. Vue 项目实战</li></ul>`,
  34. })
  35. const ReactArticle = Vue.component('reactArticle', {
  36. template: `<ul><li>1. React 基础学习</li><li>2. React 项目实战</li></ul>`,
  37. })
  38. const routes = [
  39. { path: '/index', component: Index },
  40. {
  41. path: '/article',
  42. component: Article ,
  43. children: [
  44. {
  45. path: 'vue',
  46. component: VueArticle ,
  47. },
  48. {
  49. path: 'react',
  50. component: ReactArticle ,
  51. }
  52. ]
  53. }
  54. ]
  55. const router = new VueRouter({
  56. routes: routes
  57. })
  58. var vm = new Vue({
  59. el: '#app',
  60. router,
  61. data() {
  62. return {}
  63. }
  64. })</script>
  65. </html>

代码解释:
HTML 代码第 12-13 行,我们定义了两个跳转链接。
HTML 代码第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。
JS 代码第 5-7 行,我们定义了组件 Index。
JS 代码第 9-17 行,我们定义了组件 Article,组件内部使用 <router-link></router-link> 定义来两个跳转链接,使用 <router-view></router-view> 来渲染匹配组件。
JS 代码第 19-21 行,我们定义了组件 VueArticle.
JS 代码第 23-25 行,我们定义了组件 ReactArticle。
JS 代码第 27-43 行,我们定义了路由数组,在 ‘/article’ 中配置来嵌套路由 children
JS 代码第 44-46 行,创建 router 实例,然后传 routes 配置。
JS 代码第 49 行,通过 router 配置参数注入路由。

2.2 定义路由地址

在上述的例子中,我们通过 ‘/article/vue’ 来访问嵌套路由,但是有时候你可能不希望使用嵌套路径,这时候我们可以对上面例子中的配置信息做一点修改:

  1. const routes = [
  2. { path: '/index', component: Index },
  3. {
  4. path: '/article',
  5. component: Article ,
  6. children: [
  7. {
  8. path: '/vueArticle',
  9. component: VueArticle ,
  10. },
  11. {
  12. path: '/reactArticle',
  13. component: ReactArticle ,
  14. }
  15. ]
  16. }
  17. ]

‘/’ 开头的嵌套路径会被当作根路,因此,我们访问 ‘/vueArticle’ 就相当于访问之前的 ‘/article/vue’。

3. 小结

本节,我们带大家学习了 VueRouter 嵌套路由的使用方法,主要知识点有以下几点:

  • 通过路由配置的 children 属性定义和使用嵌套路由。
  • 通过修改路由配置的 path 属性定义嵌套路由的跳转地址。

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读

    相关 VueRouter嵌套

    1. 前言 本小节我们介绍如何嵌套使用 VueRouter。嵌套路由在日常的开发中非常常见,如何定义和使用嵌套路由是本节的重点。同学们在学完本节课程之后需要自己多尝试配置

    相关 VueRouter传参

    1. 前言 本小节我们介绍 VueRouter 路由组件传参。包括 params 传参、query 传参的两种方式。路由传参的知识点非常重要,在日常开发中,我们经常会通过