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
  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中,使用Vue Router进行路由管理时,可以实现嵌套路由的效果。嵌套路由允许将多个视图组件嵌套在一个父级路由下。 要实现嵌套路由,你需要在父级路由组件中使用`<router-view>`标签来显示子级路由的视图。在子级路由组件中也可以再次使用`<router-view>`标签来嵌套更深层次的子级路由。 下面是一个简单的示例,演示如何在Vue 3中实现嵌套路由: ```html <!-- App.vue --> <template> <div> <router-view></router-view> </div> </template> <!-- Home.vue --> <template> <div> <h1>Home Page</h1> <router-view></router-view> <!-- 嵌套的子级路由视图将会显示在这里 --> </div> </template> <!-- About.vue --> <template> <div> <h2>About Page</h2> </div> </template> <!-- Contact.vue --> <template> <div> <h2>Contact Page</h2> </div> </template> ``` ```javascript // router.js import { createRouter, createWebHistory } from 'vue-router'; import Home from './components/Home.vue'; import About from './components/About.vue'; import Contact from './components/Contact.vue'; const routes = [ { path: '/', component: Home, children: [ { path: 'about', component: About }, { path: 'contact', component: Contact } ] } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router; ``` 在上面的示例中,`<router-view>`标签被用于App.vue组件和Home.vue组件中,以显示对应的子级路由视图。当访问根路径时,将会显示Home.vue组件的内容,并且在Home.vue组件中的`<router-view>`标签处显示子级路由(About.vue和Contact.vue)的内容。 希望这个示例能够帮助你理解Vue 3中如何实现嵌套路由。如果有任何疑问,请随时追问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值