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>

运行效果

相关推荐
RuoyiOffice2 天前
SpringBoot3+Vue3 最推荐的开源 OA 系统 2026:流程驱动、资源闭环、多端互通
spring boot·vue3·flowable·oa·协同办公·spring boot 3·开源oa
啊阿狸不会拉杆2 天前
《计算机网络-自顶向下方法》5.2 路由选择算法 读书笔记
计算机网络·算法·路由
鱼与宇3 天前
Element-plus离线访问(适用内网)
elementui·vue3
我就是DaLing呀!5 天前
vue3 + 独立的数据管理 Store实现视频剪辑功能
前端·typescript·vue3·canvas·store
我才是银古8 天前
当 360° 全景遇上真实地图:Vue 3 实现全景-地图双向联动的工程实践
3d·vue3·360影像
XLYcmy9 天前
HTML/CSS/JS 基础与 Vue 技术栈深度解析
java·前端·css·html·vue3·vue2·api
码视野12 天前
基于 Spring Boot + Vue3 的【大学英语四六级 (CET-4/6) 作文智能评分与句式润色系统】设计与实现(含PRD/三端高保真源码/大屏)
java·前端·人工智能·spring boot·后端·vue3
码视野12 天前
基于 Spring Boot + Vue3 的【高校化学实验室安全准入考试与危化品配伍排查系统】设计与实现(含PRD/三端高保真源码/大屏)
前端·人工智能·spring boot·后端·安全·vue3
fxshy12 天前
WebGIS 游戏化实践:基于 Cesium + Vue3 实现全球飞行模拟系统:从球体坐标、飞行动力学到地形碰撞实战
游戏·vue3·cesium·webgis·飞行模拟
jonyleek13 天前
技术实践:基于 Vue3 + Spring Cloud 微服务实现私有化文档系统的类 SaaS 协同体验
微服务·私有化部署·vue3·springcloud·协同编辑·jvs企业文档·无忧企业文档