1.定义子组件Tabs
复制
<view class="tab_wrapper">
<view class="tab">
<view class="{{currentIndex===index?'active':''}} title" wx:for="{{tabs}}" wx:key="id" bindtap="itemTap" data-index="{{index}}">
{{item.name}}
</view>
</view>
<view class="tabs_content">
<slot></slot>
</view>
</view>
复制
Component({
properties: {
tabs: {
type: Array,
value: [],
},
},
data: {
currentIndex: 0,
},
methods: {
itemTap(e) {
const index = e.currentTarget.dataset.index;
this.setData({
currentIndex: index,
});
this.triggerEvent('itemTap', { index });
},
},
});
2.使用子组件
复制
<view>
<Tabs tabs="{{tabs}}" binditemTap="ItemTap">
<block wx:if="{{currentIndex===0}}">0</block>
<block wx:if="{{currentIndex===1}}">1</block>
<block wx:if="{{currentIndex===2}}">2</block>
</Tabs>
</view>
复制
Page({
data: {
tabs: [
{ id: 0, name: '综合' },
{ id: 1, name: '销量' },
{ id: 2, name: '价格' },
],
currentIndex: 0,
},
ItemTap(e) {
this.setData({
currentIndex: e.detail.index,
});
},