Vue3-30-路由-嵌套路由的基本使用

什么是嵌套路由

嵌套路由 :就是一个组件内部还希望展示其他的组件,使用嵌套的方式实现页面组件的渲染。
就像 根组件 通过路由渲染 普通组件一样,嵌套路由也是一样的道理。

嵌套路由的相关关键配置

1、<router-view> 标签 声明 被嵌套组件 的渲染位置;
2、路由配置表中使用 children:[] 来声明 嵌套的子路由;
3、子路由 的path属性 中不可以带 /,否则无法匹配!
4、嵌套路由可以无限嵌套。

嵌套路由的语法格式

  {
        path:'/a', // 父级路由path 要有 / 
        name:'aroute',
        component:componentA,
        children:[
            {
                path:'b', // 子路由的path中 不可以有 /
                name:'broute',
                component:componentB,
            },
			
			... 还可以添加其他的子路由对象
        ]
    }

写一个案例来看看

案例的项目结构

projectName
	| -- src
		| -- App.vue
		| - componentA.vue
		| - componentB.vue
		| - componentC.vue
		| -- router.ts # 路由的配置文件
	| -- index.html

路由配置 router.ts

// 导入 定义路由的两个方法
import {createRouter,createWebHistory}  from 'vue-router'

// 引入两个组件
import componentA from "./componentA.vue";
import componentB from "./componentB.vue";
import componentC from "./componentC.vue";

// 声明路由跳转的路径与组件的对应关系
const routsList = [
    {
        path:'/a',
        name:'aroute',
        component:componentA,
        children:[
            {
                path:'b',
                name:'broute',
                component:componentB,
                children:[
                    {
                        path:'c',
                        name:'croute',
                        component:componentC
                    },
                ]
            },
        ]
    },
]

// 创建路由的实例对象
const routerConfigObj = createRouter({
    history:createWebHistory('abc'), // 带一个参数,表示是路由的一个前缀
    routes:routsList // 指定路由的配置列表
})

// 导出路由的对象
export default routerConfigObj;

App.vue 中的跳转逻辑代码

有一个按钮,点击跳转到一个嵌套的路由中去

<template>
    <div class="basediv">
      
        APP.vue 中的 msg : {{ msg }}

        <br>   
        <button @click="pushToB">路由到组件B</button>
        <!-- <br> -->
        <br>
        <br><br><br>
        <!-- router-view 进行目标组件的展示 -->
        <router-view></router-view>
    
    </div>
   
</template>
    
<script setup lang="ts">

	// 引入 provide 方法
    import { ref } from 'vue'

    // 引入路由配置实例 与 当前路由对象
    import { useRouter } from 'vue-router';

    // 声明父组件的一个变量
    const msg = ref('这是App根组件的msg变量')

    // 接收一下路由实例对象 和 当前路由 对象
    const routerObj = useRouter();

    // 路由跳转到 B组件的处理方法
    const pushToB = ()=>{
        routerObj.replace({
            path:'/a/b',
            query:{
                p3:'vue3',
                p4:'路由的跳转'
            }
        })
    }    
    
</script>
    
<style scoped>

    .basediv{
        width: 600px;
        height: 400px;
        border: 1px solid red;
    }
</style>

componentA.vue

componentB.vue 和componentA.vue的代码一致;
componentC.vue 中 没有 <router-view>标签

<template>
    <div class="diva">
        这是组件A
        <br>
        <!-- 子组件的展示位置 -->
        <router-view></router-view>
    </div>
    
</template>

<script setup lang="ts">

    // 引入两个路由相关的方法
    import { useRouter,useRoute} from 'vue-router';

    // 声明 路由实例对象 和 当前路由对象
    const routerObj = useRouter()
    const currentRoute = useRoute()

    // 打印一下路由实例对象 和 当前路由对象
    console.log('A 组件 中 路由实例对象 :',routerObj)
    console.log('A 组件 中 当前路由对象 :',currentRoute)


</script>

<style scoped>
    .diva{
        width: 300px;
        height: 200px;
        background: red;
    }
</style>

运行效果

1、初始状态

在这里插入图片描述

2、浏览器地址栏中直接输入地址进行路由

在这里插入图片描述

3、点击按钮进行路由

在这里插入图片描述

  • 16
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router 是一个官方提供的 Vue.js 的路由管理器,可以用于构建单页面应用程序。嵌套路由是指在一个路由的组件中使用另一个路由。 在 Vue Router 中,可以通过在路由配置文件中定义嵌套路由嵌套路由的配置是以树形结构来组织的,父级路由将会嵌套渲染其子路由的组件。 下面是一个示例的路由配置文件,演示了如何使用嵌套路由: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) const routes = [ { path: '/', component: Home, children: [ { path: '', component: Dashboard }, { path: 'about', component: About }, { path: 'products', component: Products, children: [ { path: '', component: ProductList }, { path: ':id', component: ProductDetail } ] } ] } ] const router = new VueRouter({ routes }) export default router ``` 在上面的代码中,父级路由 '/' 下包含了三个子路由:Dashboard、About 和 Products。而 Products 路由又包含了两个子路由:ProductList 和 ProductDetail。 在组件中使用嵌套路由时,需要在父级组件中使用 `<router-view>` 标签来渲染子路由的内容。 ```html <template> <div> <h1>Home</h1> <router-view></router-view> </div> </template> ``` 在父级组件的模板中,通过使用 `<router-view>` 标签,子路由的内容将会被渲染在这个位置。 这就是 Vue Router 中嵌套路由基本使用方法。通过嵌套路由,可以更好地组织和管理应用程序的路由结构,实现更复杂的页面布局和导航功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值