
Tabs组件:
<slot/><scroll-view class="scroll-view-x" scroll-x ><view class="scroll-view-item" wx:for="{{tabList}}" wx:key="name"><view class="{{currentTab === index ? 'on' : ''}}" bind:tap="_switchTab" data-current="{{item.page}}">{{item.name}}</view></view></scroll-view>
// components/tabs/tabs.jsComponent({/*** 组件的属性列表*/properties: {tabList: {type: Array,value: []},currentTab: {type: Number,value: 0,observer: function(newVal, oldValue, data){// 该方法当此数据被setData的时候被调用console.log(newVal, oldValue, data);}}},/*** 组件的初始数据*/data: {},/*** 组件的方法列表*/methods: {_switchTab: function(e){console.log("_switchTab",e);this.triggerEvent("changeTab", {currentNum: e.currentTarget.dataset.current});}}})
.scroll-view-x {background-color: #fff;white-space: nowrap;/* position: fixed; */z-index: 10;top: 0px;}.scroll-view-x .scroll-view-item {display: inline-block;margin: 0 35rpx;line-height: 33px;cursor: pointer;}.on{border-bottom: 2px solid red;color: red;}
{"component": true,"usingComponents": {}}
在页面中使用:
<tabs currentTab="{{currentTab}}" tabList="{{statusType}}" bind:changeTab="switchTab"><swiper current="{{currentTab}}" duration="300" bindchange="bindChange" style="height:{{windowHeight-35}}px;"><swiper-item wx:for="{{list}}" wx:key="*this"><view>{{item}}</view></swiper-item></swiper></tabs>
// pages/componentPage/componentPage.jsPage({/*** 页面的初始数据*/data: {statusType: [{name: "待付款",page: 0},{name: "待发货",page: 1},{name: "待收货",page: 2},{name: "待评价",page: 3},{name: "已完成",page: 4}],currentTab: 0,list: ["page1", "Page2", "Page3", "Page4", "Page5"],windowHeight: ''},/*** 生命周期函数--监听页面加载*/onLoad: function (options) {this.setData({windowHeight: wx.getSystemInfoSync().windowHeight});},switchTab: function(event){console.log("page",event.detail.currentNum);if (this.data.currentTab === event.detail.currentNum) {return;}this.setData({currentTab: event.detail.currentNum});},bindChange: function(event){this.setData({currentTab: event.detail.current});},/*** 生命周期函数--监听页面初次渲染完成*/onReady: function () {},/*** 生命周期函数--监听页面显示*/onShow: function () {},/*** 生命周期函数--监听页面隐藏*/onHide: function () {},/*** 生命周期函数--监听页面卸载*/onUnload: function () {},/*** 页面相关事件处理函数--监听用户下拉动作*/onPullDownRefresh: function () {},/*** 页面上拉触底事件的处理函数*/onReachBottom: function () {},/*** 用户点击右上角分享*/onShareAppMessage: function () {}})
{"usingComponents": { "tabs": "/components/tabs/tabs"}}
