tab切换子路由和子组件

15 篇文章 0 订阅

命名组件:适用于各组件都不同的情况

在这里插入图片描述

  • 通过 Vue 的 元素加一个特殊的 :is=“active” 来实现组件的切换
  • 将tab的name 设置和组件名字一样,这样active就是当前显示组件的name
<template>
  <div class="app-container">
    <el-tabs v-model="active" type="card" @tab-click="handleClick">
      <el-tab-pane label="基本资料" name="basicInfo"></el-tab-pane>
      <el-tab-pane label="一般服务" name="generalService"></el-tab-pane>
      <el-tab-pane label="急救服务" name="emergencyService"></el-tab-pane>
      <el-tab-pane label="服务组织订单" name="orderService"></el-tab-pane>
      <el-tab-pane label="投诉建议" name="complaint"></el-tab-pane>
      <el-tab-pane label="回访客户" name="returnVisit"></el-tab-pane>
      <el-tab-pane label="通话记录" name="telRecord"></el-tab-pane>
      <el-tab-pane label="地图定位" name="mapPosition"></el-tab-pane>
      <el-tab-pane label="健康管理" name="health"></el-tab-pane>
      <el-tab-pane label="评估管理" name="assess"></el-tab-pane>
      <el-tab-pane label="智能监测" name="monitor"></el-tab-pane>
    </el-tabs>
    <component :is="active"></component>
  </div>
</template>

<script>
export default {
  data() {
    return {
      active: "basicInfo",
    };
  },
  components: {
    basicInfo: () => import("./components/basicInfo.vue"),
    generalService: () => import("./components/generalService.vue"),
    emergencyService: () => import("./components/emergencyService.vue"),
    orderService: () => import("./components/orderService.vue"),
    complaint: () => import("./components/complaint.vue"),
    returnVisit: () => import("./components/returnVisit.vue"),
    telRecord: () => import("./components/telRecord.vue"),
    mapPosition: () => import("./components/mapPosition.vue"),
    health: () => import("./components/health.vue"),
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
  },
};
</script>

方法一:子路由,适用于完全不同的情况

//vant框架tab组件,结合to属性
<template>
  <div class="audit_2">
    <van-tabs v-model="active" sticky>
      <van-tab title="代理审核" to="/audit_2/audit" name="/audit_2/audit">
        <router-view></router-view>
      </van-tab>
      <van-tab
        title="下级充值"
        to="/audit_2/topuplist"
        name="/audit_2/topuplist"
      >
        <router-view></router-view>
      </van-tab>
    </van-tabs>
  </div>
</template>

解决tab切换bug

//name设置为路由,active也为路由,通过动态设置active为当前路由,从而达到页面和tab对应的效果
data() {
    return {
      active: "/audit_2/audit"
    };
  },
  watch: {        // bug2:点击返回按钮,tab跟页面不对应
    $route: function(newval, oldval) {
      this.active = newval.path;
    }
  },
  mounted() {
    this.active = this.$route.path;    // bug1:刷新页面,tab跟页面不对应
  },
//路                                                                                                                                                                                                                                                                                                                  由配置,先引入
//bug:子路由的path不能加/,路由会自动配置
    {
      path: "/audit_2", //代理2.0版本 —— 审核
      name: "audit_2",
      component: audit_2,
      redirect: "/audit_2/audit",
      children: [
        {
          path: "audit",
          name: "audit",
          component: audit,
          meta: {
            title: "审核授权"
          }
        },
        {
          path: "topuplist",
          name: "topuplist",
          component: topuplist,
          meta: {
            title: "下级充值审核"
          }
        }
      ]
    },

方法二:子组件,适用于组件相同或接口相同的情况

<template>
  <div class="bonus_payout">
    <van-tabs v-model="active">
      <van-tab title="推荐奖"><tuijian></tuijian></van-tab>
      <van-tab title="业绩奖"><yeji></yeji></van-tab>
      <van-tab title="平级奖"><pingji></pingji></van-tab>
    </van-tabs>
  </div>
</template>

<script>
import tuijian from "./_components/tuijian_list";
import yeji from "./_components/yeji_list";
import pingji from "./_components/pingji_list";
export default {
  components: {
    tuijian,
    yeji,
    pingji
  },
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值