uview 中u-tabs如何做tab切换?

本文介绍了一个使用uni-app实现的标签页切换组件案例。通过简单的代码示例展示了如何使用u-tabs组件进行页面内容的动态切换,并配置了橙色作为选中状态的颜色,同时提供了两个示例标签页的内容展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看案例如图所示

话不多说上代码:

<template>
  <view class="treeroot">
    <view class="">
      <u-tabs 
      :list="list" 
      :is-scroll="false" 
      :current="current" 
      bar-width="50" 
      active-color="orange" 
      @change="change">
      </u-tabs>
      <view v-if="current == 0">
        11
      </view>
      <view v-if="current == 1">
        22
      </view>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        //
        list: [{
            name: "额度恢复记录",
          },
          {
            name: "还款记录",
          },
        ],
        //默认展开第几项
        current: 0,
      };
    },
    methods: {
      change(index) {
        this.current = index;
        //如报错则用this.current = index.index;代替上面这句
      },
    },
  };
</script>

<style scoped lang="scss">
 
</style>

 

 

 

 

这个问题可能需要更详细的描述和代码示例才能够理解你的问题。不过,根据你的描述,我猜测你可能是想实现一个带有 `u-tabs-swiper` 和 `swiper` 组件的页面,当用户左右滑动页面时,`u-tabs-swiper` 组件会自动切换到对应的选项卡。 如果这是你的需求,你可以尝试在 `swiper` 组件上监听滑动事件,并在事件处理程序中手动调用 `u-tabs-swiper` 组件的 `setActiveTab` 方法来切换选项卡。具体代码示例如下: ```html <template> <view> <!-- u-tabs-swiper 组件 --> <u-tabs-swiper :tabs="tabs" ref="tabsSwiper" @tab-click="handleTabClick" /> <!-- swiper 组件 --> <swiper @change="handleSwiperChange"> <swiper-item v-for="(item, index) in tabs" :key="index"> <!-- 页面内容 --> </swiper-item> </swiper> </view> </template> <script> export default { data() { return { tabs: ['选项卡1', '选项卡2', '选项卡3'], activeTab: 0, }; }, methods: { // u-tabs-swiper 组件的选项卡点击事件处理程序 handleTabClick(tab, index) { this.activeTab = index; // 切换到对应的 swiper 页面 this.$refs.swiper.swiper.slideTo(index); }, // swiper 组件的页面切换事件处理程序 handleSwiperChange() { const activeIndex = this.$refs.swiper.swiper.activeIndex; if (activeIndex !== this.activeTab) { // 切换到对应的选项卡 this.$refs.tabsSwiper.setActiveTab(activeIndex); this.activeTab = activeIndex; } }, }, }; </script> ``` 在上面的代码中,我们在 `swiper` 组件上监听了 `change` 事件,并在事件处理程序中手动调用 `u-tabs-swiper` 组件的 `setActiveTab` 方法来切换选项卡。同时,在 `u-tabs-swiper` 组件上监听了 `tab-click` 事件,当用户点击选项卡时,我们也需要手动调用 `swiper` 组件的 `slideTo` 方法来切换页面。 希望这个代码示例能够解决你的问题。如果还有其他问题,欢迎继续提问!
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王工丶要专注

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

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

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

打赏作者

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

抵扣说明:

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

余额充值