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>

运行效果

相关推荐
Sheldon一蓑烟雨任平生3 小时前
Vue3 列表渲染
vue.js·vue3·v-for·列表渲染·vue3 列表渲染·v-for 循环对象·v-for与计算属性
花菜会噎住6 小时前
Vue3 路由配置和使用与讲解(超级详细)
开发语言·javascript·ecmascript·路由·router
duansamve11 小时前
TS在Vue3中的使用实例集合
typescript·vue3
ღ᭄ꦿ࿐Never say never꧂14 小时前
微信小程序 Button 组件 open-type 完全指南:从用户信息获取到客服分享的实战应用
spring boot·微信小程序·小程序·uni-app·vue3
Sheldon一蓑烟雨任平生1 天前
Vue3 Class 与 Style 绑定
vue.js·vue3·class与style绑定·绑定class·绑定style·vue3绑定class·vue3绑定style
昔冰_G10 天前
Vue内置组件KeepAlive——缓存组件实例
vue.js·缓存·vue3·vue2·keep-alive·vue组件缓存·vue内置组件
wxr061612 天前
部署Spring Boot项目+mysql并允许前端本地访问的步骤
前端·javascript·vue.js·阿里云·vue3·springboot
liulilittle13 天前
macOS 内核路由表操作:直接 API 编程指南
网络·c++·macos·策略模式·路由·route·通信
知识分享小能手13 天前
微信小程序入门学习教程,从入门到精通,微信小程序开发进阶(7)
前端·javascript·学习·程序人生·微信小程序·小程序·vue3
上单带刀不带妹19 天前
Vue3 全局 API 转移详解
前端·javascript·vue.js·vue3·api