自定义tabs组件

文档参考

自定义组件 | 微信开放文档

  • tabs.json
{  "component": true,  "usingComponents": {}}
  • tabs.wxml
<view class="tabs">
    <view class="tabs_title">
        <view wx:for="{{tabList}}" wx:key="id" class="title_item  {{index==tabIndex?'item_active':''}}" bindtap="handleItemTap" data-index="{{index}}">
            <view style="margin-bottom:5rpx">{{item}}</view>
            <view style="width:30px" class="{{index==tabIndex?'item_active1':''}}"></view>
        </view>
    </view>
    <view class="tabs_content">
        <slot></slot>
    </view>
</view>
​
  • tabs.wxss
.tabs {
    position: fixed;
    top: 0;
    width: 100%;
    background-color: #fff;
    z-index: 99;
    border-bottom: 1px solid #efefef;
    padding-bottom: 20rpx;
}
​
.tabs_title {
    /* width: 400rpx; */
    width: 90%;
    display: flex;
    font-size: 9pt;
    padding: 0 20rpx;
}
​
.title_item {
    color: #999;
    padding: 15rpx 0;
    display: flex;
    flex: 1;
    flex-flow: column nowrap;
    justify-content: center;
    align-items: center;
}
​
.item_active {
    /* color:#ED8137; */
    color: #000000;
    font-size: 11pt;
    font-weight: 800;
}
​
.item_active1 {
    /* color:#ED8137; */
    color: #000000;
    font-size: 11pt;
    font-weight: 800;
    border-bottom: 6rpx solid #333;
    border-radius: 2px;
}
  • tabs.js
var App = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabList:Object
  },
​
  /**
   * 组件的初始数据
   */
  data: {
    tabIndex:0
  },
​
  /**
   * 组件的方法列表
   */
  methods: {
    handleItemTap(e){
      // 获取索引
      const {index} = e.currentTarget.dataset;
      // 触发 父组件的事件
      this.triggerEvent("tabsItemChange",{index})
      this.setData({
          tabIndex:index
      })
    }
  }
})
​

使用组件

  • list.json
{
    "usingComponents": {
      "tabs":"/components/tabs/tabs"
    }
}
  • list.wxml
<tabs tabList="{{tabs}}"  bindtabsItemChange="tabsItemChange">
</tabs>

此名称 tabs与 json文件中的名称对应

  • list.js
// pages/meeting/list/list.js
Page({
​
    /**
     * 页面的初始数据
     */
    data: {
      tabs:['会议中','已完成','已取消','全部会议'], //定义组件 的类容
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
​
    },
​
​
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
​
    },
​
    tabsItemChange(e){
      //  e.detail.index 获取taba组件中传过来的 index
        let tolists;
        if(e.detail.index==1){
          // 点击第二个菜单操作
        }else if(e.detail.index==2){
          
        }else if(e.detail.index==3){
        
        }else{
           
        }
    }
})

实现效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的自定义tabs组件的实现示例: ```vue <template> <div class="tabs"> <div class="tab-header"> <div v-for="(tab, index) in tabs" :key="index" :class="{ active: currentIndex === index }" @click="changeTab(index)" > {{ tab.title }} </div> </div> <div class="tab-content"> <slot /> </div> </div> </template> <script> export default { name: "Tabs", props: { defaultIndex: { type: Number, default: 0, }, }, data() { return { currentIndex: this.defaultIndex, tabs: [], }; }, methods: { changeTab(index) { this.currentIndex = index; }, registerTab(tab) { this.tabs.push(tab); }, }, mounted() { this.tabs = this.$children; }, }; </script> ``` 在这个组件中,我们使用了两个插槽,一个是tab-header用于显示标签页标题,另一个是tab-content用于显示标签页内容。组件内部维护了一个tabs数组,用于存储所有的标签页组件。 标签页组件的实现如下: ```vue <template> <div v-show="active"> <slot></slot> </div> </template> <script> export default { name: "Tab", props: { title: { type: String, required: true, }, }, data() { return { active: false, }; }, mounted() { this.$parent.registerTab(this); }, updated() { if (this.$parent.currentIndex === this.$parent.tabs.indexOf(this)) { this.active = true; } else { this.active = false; } }, }; </script> ``` 在标签页组件中,我们维护了一个active属性,用于控制标签页是否显示。同时,当标签页组件的父组件Tabs更新时,我们会根据当前标签页在tabs数组中的位置和父组件当前激活的标签页的位置来更新active属性。 使用方式如下: ```vue <template> <div> <tabs> <tab title="Tab 1"> <p>Content of Tab 1</p> </tab> <tab title="Tab 2"> <p>Content of Tab 2</p> </tab> <tab title="Tab 3"> <p>Content of Tab 3</p> </tab> </tabs> </div> </template> <script> import Tabs from "@/components/Tabs.vue"; import Tab from "@/components/Tab.vue"; export default { name: "App", components: { Tabs, Tab, }, }; </script> ``` 其中,Tabs组件包含了三个Tab组件,每个Tab组件都有一个标题和内容。可以根据需要自行修改样式和内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值