vue3 - 36.router.6 - 嵌套路由

嵌套路由的使用场景:

比如手机端,底部有四个或者五个子路由页面,当我们点击底部路由的时候,页面进行切换

或者我们常见的后台管理系统,左侧为导航,右侧为导航对应的页面内容

1.嵌套路由可以通过children的方式来声明,children接收的是一个数组,数组中可以定义无限个子路由

我们新建三个路由页面,并且在router/index.ts中声明嵌套路由:

import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
const routes:Array<RouteRecordRaw> = [
    {
        path:'/index',
        name:'main',
        component:() => import('../components/Main/index.vue'),
        children:[
            {
                path:"/index",
                name:"index",
                component:() => import('../views/index/index.vue'),
            },{
                path:"/classify",
                name:"classify",
                component:() => import('../views/classify/index.vue'),
            },{
                path:"/me",
                name:"me",
                component:() => import('../views/me/index.vue'),
            }
        ]
    }
]
const router = createRouter({
    history:createWebHistory(),
    routes
})
export default router

我们再单独新建一个承载整个项目的总路由,比如mian.vue

main.vue:

<template>
    <div>
        <RouterView />
        <br />
        <RouterLink :to="{path:'/index'}">首页</RouterLink>
        <br />
        <RouterLink :to="{path:'/classify'}">分类</RouterLink>
        <br />
        <RouterLink :to="{path:'/me'}">我的</RouterLink>
    </div>
</template>
<script setup lang='ts'>
  import { RouterView,RouterLink } from 'vue-router';
</script>

我们可以把底部跳转封装成一个路由子组件并且引用,css布局样式略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值