Vue3全家桶 - VueRouter - 【3】嵌套路由【children】

嵌套路由【children

  • 如果在路由视图中展示的组件包含自己的路由占位符(路由出口),则此处会用到嵌套路由;
  • 如图所示:点击关于链接,则会展示About组件,在其组件中又包含了路由链接和路由占位符;
    image.png
  • 路由嵌套规则
    • 某一个路由规则中采用 children 来声明嵌套路由的规则;
    • 嵌套路由规则中的 path 不能以 / 开头,访问需要使用过 /fatherPath/sonPath 的形式;
  • 示例展示:
    • 路由模块 - router/index.js
      import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
      
      // TODO 创建路由规则数组
      const routes = [
        {
          path: '/',
          // 路由重定向
          redirect: '/guoMan'
        },
        {
          path: '/teleplay',
          name: 'teleplay',
          component: () => import('@/views/Teleplay/index.vue'),
          children: [
            {
              path: 'teleplayList',
              name: 'teleplayList',
              component: () => import('@/views/Teleplay/components/TeleplayList.vue')
            }
          ]
        },
        {
          path: '/guoMan',
          name: 'guoMan',
          component: () => import('@/views/GuoMan/index.vue'),
          children: [
            {
              path: 'guoManList',
              name: 'guoManList',
              component: () => import('@/views/GuoMan/components/GuoManList.vue')
            }
          ]
        },
        {
          path: '/riMan',
          name: 'riMan',
          component: () => import('@/views/RiMan/index.vue'),
          children: [
            {
              path: 'riManList',
              name: 'riManList',
              component: () => import('@/views/RiMan/components/RiManList.vue')
            }
          ]
        },
        {
          path: '/movie',
          name: 'movie',
          component: () => import('@/views/Movie/index.vue'),
          children: [
            {
              path: 'movieList',
              name: 'movieList',
              component: () => import('@/views/Movie/components/MovieList.vue')
            }
          ]
        }
      ]
      
      // TODO 创建路由
      const router = createRouter({
        // TODO 规定路由模式
        // history: createWebHashHistory(),
        history: createWebHistory(),
        routes
      })
      
      export default router
      
    • 文档结构展示:
      image.png
    • 只展示一个目录中的,其他目录的都一样:
      • views/GuoMan/index.vue
        <script setup>
        import { ref, reactive, computed, onMounted } from 'vue'
        
        onMounted(() => {});
        </script>
        
        <template>
          <h3>国漫</h3>
          <router-link to="/guoMan/guoManList" class="router-link">展示国漫列表</router-link>
          <hr>
          <router-view />
        </template>
        
        <style scoped>
        h3 {
          color: #fff;
          font-size: 30px;
          font-family: '隶书';
        }
        
        .router-link {
          padding: 0 10px;
          color: #fff;
          font-size: 24px;
          font-family: '隶书';
        }
        </style>
        
      • views/GuoMan/components/GuoManList.vue:
        <script setup>
        import { ref, reactive, computed, onMounted } from 'vue'
        let list = ref([
          {
            id: 'w45',
            title: '完美世界',
          },
          {
            id: 'y43',
            title: '一念永恒'
          },
          {
            id: 'z27',
            title: '赘婿'
          }
        ])
        onMounted(() => {});
        </script>
        
        <template>
          <ul>
            <li v-for="({id, title}) in list" :key="id"> {{ title }} </li>
          </ul>
        </template>
        
        <style scoped>
        li {
          list-style: none;
          padding: 0 10px;
          color: yellow;
          font-size: 24px;
          font-family: '隶书';
        }
        </style>
        
  • 运行展示:
    5876dd7e-1f55-4c98-ad73-7476eea05676.gif

相关推荐

  1. [Vue3] 嵌套

    2024-03-13 01:30:03       41 阅读
  2. Vue 3嵌套

    2024-03-13 01:30:03       37 阅读
  3. vue3

    2024-03-13 01:30:03       70 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 01:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 01:30:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 01:30:03       82 阅读
  4. Python语言-面向对象

    2024-03-13 01:30:03       91 阅读

热门阅读

  1. 【More Effective C++】条款24:了解虚函数的成本

    2024-03-13 01:30:03       52 阅读
  2. html 如何引入 百度地图

    2024-03-13 01:30:03       41 阅读
  3. 前端面试练习24.3.11

    2024-03-13 01:30:03       37 阅读
  4. MYSQL环境搭建面试题

    2024-03-13 01:30:03       34 阅读
  5. PTA L1-009 N个数求和(C++)

    2024-03-13 01:30:03       39 阅读
  6. url中可以包含@吗

    2024-03-13 01:30:03       39 阅读
  7. docker学习笔记——对数据卷的一些简单命令

    2024-03-13 01:30:03       43 阅读
  8. Activiti工作流引擎:流程实例名称模糊查询

    2024-03-13 01:30:03       44 阅读