my-tab.js
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36// component/my-tab/my-tab.js Component({ /** * 组件的属性列表 */ properties: { tabArr:{ type:Array, value:[] } }, /** * 组件的初始数据 */ data: { currentIndex:0 }, /** * 组件的方法列表 */ methods: { tabClick(e){ this.setData({ currentIndex:e.currentTarget.dataset.index }) console.log(this.data.currentIndex); this.triggerEvent('tab',{ tab_index: this.data.currentIndex, title: this.data.tabArr[this.data.currentIndex] }) } } })
my-tab.wxss
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/* component/my-tab/my-tab.wxss */ .tab-box { display: flex; background: rgb(240, 239, 239); height: 100rpx; line-height: 100rpx; } .tab-item { flex: 1; text-align: center; } .tab-box .active { color: red; }
my-tab.wxml
复制代码
1
2
3
4
5
6
7
8
9<!--component/my-tab/my-tab.wxml--> <view class="tab-box"> <block wx:for="{{tabArr}}" wx:key="index"> <view class="tab-item {{currentIndex == index?'active':''}}" bindtap="tabClick" data-index="{{index}}"> <text>{{item}}</text> </view> </block> </view>
在页面中的·使用
复制代码
1
2
3
4
5
6<!--index.wxml--> <view class="container"> <!-- tab标签栏实战 --> <my-tab bindtab="itemClick" tabArr="{{cates}}"></my-tab> </view>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20//index.js //获取应用实例 const app = getApp() Page({ data: { title:"这是页面传的数据", cates:['推荐','热门','时尚','娱乐'] }, onLoad: function () { }, search:function(e){ console.log(e.detail.searchText); }, itemClick: function(e){ console.log(e.detail); } })
最后
以上就是怕黑水壶最近收集整理的关于微信小程序(tab栏的封装和使用)的全部内容,更多相关微信小程序(tab栏内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复