vue-router 命名视图案例

有的时候想在页面中同时展示多个视图,而不是切换形式的嵌套展示,这个时候拥有多个出口的router-view就能很方便的实现;
现在有这么一个场景:有一个一个页面由左侧侧边栏tab页组成,右边是对应tab所显示的内容,其中一个tab的内容由2个组件构成,那么使用命名视图该如何实现?
下面就是实现的过程:

1、创建good-children.vue文件

<template>
  <div class="box">
    <div class="btns">
      <router-link to="/class1" router-link-active="active">班级1的优秀学生信息</router-link>
      <br>
      <router-link to="/class2">班级2的优秀学生信息</router-link>
    </div>
    <div class="children">
      <!--有多个出口的router-view 没有指定name的router-view默认值为default,指定name的需要与路由配置index.js中的值一致-->
      <router-view></router-view>
      <router-view name="studentExact"></router-view>
    </div>
  </div>

</template>

<script>
export default {}
</script>

<style scoped>
  .box{
    display: flex;
    justify-content: space-around;
  }
  .box .btns{
    width: 20%;
    border:1px solid #000;
    height: 200px;
    line-height: 100px;
    text-align: center;
  }
  .box .children{
    width: 80%;
    border:1px solid #000;
    height: 200px;
    line-height: 100px;
    text-align: center;
  }
  .router-link-active{
    background: #ffc325;
    border: 1px solid #ffc325;
    padding:5px 8px;
    border-radius: 5px;
    text-align: center;
    text-decoration: none;
  }
</style>

2、创建children1.vue文件

<template>
  <div>
    children1 用户详情
  </div>
</template>

<script>
export default {}
</script>

<style scoped></style>

3、创建children2.vue文件

<template>
  <div>
    children2 用户详情
  </div>
</template>

<script>
export default {}
</script>

<style scoped></style>

4、创建children3.vue文件

<template>
  <div>
    children3 用户详情
  </div>
</template>

<script>
export default {}
</script>

<style scoped></style>

5、对路由文件index.js进行命名视图路由的配置

import Vue from 'vue';
import Router from 'vue-router';
import goodChildren from '@/components/good-children';
import children1 from '@/components/children1';
import children2 from '@/components/children2';
import children3 from '@/components/children3';

Vue.use(Router);

export default new Router({
  routes: [
    //命名视图的路由配置
    {
      path:'/good-children',
      component:goodChildren,
      children:[
        {
          path:'/class1',
          component:children1
        },
        {
          path:'/class2',
          components:{
            default:children2,
            studentExact:children3
          }
        }
      ]
    }
  ]
})

6、显示效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值