Vue3-31-路由-RouterView的name属性的作用

作用描述

<router-view> 标签是用来渲染路由对应的组件的位置;

默认情况下,一个路由是只对应一个组件的。

但是,可以通过给 <router-view> 指定 name 属性的方式,实现同时渲染多个组件的效果。

这也叫做 命名视图。

注意点

1、因为一个路由需要对应多个组件,所以在进行路由配置时需要使用 components 来代替 component;
2、在使用<router-view> 时指定 name 属性,不指定的,默认是 default;
3、components 中使用 key-value 的形式配置组件,
key : 就是 <router-view> 的 name 属性的值
value : 就是要渲染的组件

复制代码
路由中的配置就像下面这样 :

  {
        path:'/showAllComponents',
        name:'aroute',
        components:{ // 指定多个组件
            default : componentA,  // 默认名称 default
            first : componentB, // name = first 的 router-view
            second : componentC  // name = second 的 router-view
        },
       
    },

使用案例

复制代码
项目结构如下 :
projectName
	| -- src
		| -- App.vue # 根组件,A B C 三个组件都在这里展示
		| -- componentA.vue
		| -- componentB.vue
		| -- componentC.vue
		| -- router.ts # 路由配置的文件
		| -- mian.ts # 程序的入口ts文件
	| -- index.html # 项目页面入口文件

下面的案例代码只展示重点的部分:

router.ts 路由配置

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:'/showAllComponents',
        name:'aroute',
        components:{
            default : componentA,
            first : componentB,
            second : componentC
        },
    },
]

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

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

App.vue 根组件内容

html 复制代码
<template>
    <div class="basediv">
      
        APP.vue 中的 msg : {{ msg }}
        
        <br><br><br>
        <!-- router-view 进行目标组件的展示 -->
        <router-view></router-view>
        <router-view name="first"></router-view>
        <router-view name="second"></router-view>
    
    </div>
   
</template>
    
<script setup lang="ts">

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

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

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

运行效果

相关推荐
RuoyiOffice14 小时前
SpringBoot+Vue3 业财协同架构:预算、费用分摊、发票、往来与凭证怎样避免五套数字
spring boot·vue3·财务管理·spring boot 3·预算管理·ruoyi office·业财协同
RuoyiOffice2 天前
Vue3+TypeScript 企业管理平台统一UI:新增一个模块,为什么还要重新调样式?
spring boot·typescript·vue3·组件化开发·企业管理软件·ruoyi office·统一ui
Shockang3 天前
TanStack Router 实战:URL 校验、Loader 与路由权限
typescript·react·路由·前端开发·tanstack router
Liu.7745 天前
Vue3 中如何使用 GET 请求传递复杂参数
vue.js·vue3
RuoyiOffice6 天前
Spring AI+Vue3 企业知识库设计:让每个答案有依据,让每份资料守住权限
java·spring boot·vue3·rag·企业知识库·spring ai·ruoyi office
RuoyiOffice7 天前
SpringBoot3+Vue3 员工全生命周期:入转调离如何衔接账号、权限、资产与交接
spring boot·vue3·权限管理·员工管理·hrm·ruoyi office·人事变更
goehou7 天前
模型网关实战:多模型路由、降级和成本对账的统一入口
网关·ai·llm·路由·成本
RuoyiOffice8 天前
SpringBoot3+Vue3 企业 AI 工作流:Tinyflow 画布、LLM 节点与在线试跑怎么落地
spring boot·大模型·vue3·agent·ai工作流·ruoyi office·tinyflow
RuoyiOffice8 天前
Springboot + Vue3 + FormCreate表单引擎,直接用AI对话进行单据设计,告别拖拽画表单
spring boot·低代码·vue3·sse·formcreate·ruoyi office·ai表单
RuoyiOffice11 天前
SpringBoot+Vue3+Onlyoffice 企业文件生命周期:上传、业务关联、权限、版本与安全清理
spring boot·vue3·文件管理·对象存储·权限控制·ruoyi office·附件管理