小程序tabBar组件怎么封装,具体实现是怎样
Admin 2022-06-18 群英技术资讯 977 次浏览
这篇文章将为大家详细讲解有关“小程序tabBar组件怎么封装,具体实现是怎样”的知识,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。本文实例为大家分享了小程序自定义tabBar组件封装的具体代码,供大家参考,具体内容如下

1、新建组件:在component下新建一个tabBar
2、组件的index.wxml结构如下:
<cover-view class="tab-bar">
<cover-view class="tab-bar-border"></cover-view>
<cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="tabChange">
<cover-image src="{{tabbarIndex === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view style="color: {{tabbarIndex === index ? selectedColor : color}}">{{item.text}}</cover-view>
</cover-view>
</cover-view>
3、组件的index.wxss结构如下:
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 60px;
background: white;
display: flex;
padding-bottom: env(safe-area-inset-bottom);
}
.tab-bar-border {
background-color: rgba(0, 0, 0, 0.33);
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 1px;
transform: scaleY(0.5);
}
.tab-bar-item {
flex: 1;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.tab-bar-item cover-image {
width: 28px;
height: 28px;
margin-bottom: 2px;
}
.tab-bar-item cover-view {
font-size: 10px;
}
4、组件的index.js结构如下:
// pages/components/tabBar/index.js
Component({
/**
1. 组件的属性列表
*/
options: {
multipleSlots: true //在组件定义时的选项中启用多slot支持
},
properties: {
list: {
type: Array,
value: []
},
selectedColor:{
type: String,
value:''
},
color:{
type: String,
value:''
},
},
/**
2. 组件的初始数据
*/
data: {
tabbarIndex: 0//默认显示第一个tab元素
},
lifetimes: {
attached() {}
},
/**
3. 组件的方法列表
*/
methods: {
//组件的点击事件
tabChange(e) {
//获取到底部栏元素的下标
let index = e.currentTarget.dataset.index;
this.setData({
tabbarIndex:index,
})
//triggerEvent获取组件的事件
//onMyEvent 页面传过来的点击事件名称
this.triggerEvent('onMyEvent',{
tabbarIndex:index,
})
},
}
})
5、组件的index.json结构如下:
{
"component": true,
"usingComponents": {}
}
6、组件在页面中的使用
7、页面的json代码如下:
{
"navigationBarTitleText": "测试",
"usingComponents": {
"mp-tabbar": "../components/tabBar/index"
}
}
8、页面的wxml代码如下:
//当选中tab1时页面显示的内容
<view wx:if="{{tabbarIndex == 0}}">111111</view>
//当选中tab2时页面显示的内容
<view wx:if="{{tabbarIndex == 1}}">222222</view>
//当选中tab3时页面显示的内容
<view wx:if="{{tabbarIndex == 2}}">333333</view>
<mp-tabbar list="{{list}}" id='tabComponent' bind:onMyEvent="tabChange"></mp-tabbar>
9、页面的js代码如下:
Page({
data: {
tabbarIndex:0,//默认第一个tab元素
color: "#555555",
selectedColor: "#2ea7e0",
//底部栏
list: [{
"text": "市场",
"iconPath": "/images/bazaar.png",
"selectedIconPath": "/images/bazaar_selected.png",
},
{
"text": "充值",
"iconPath": "/images/recharge.png",
"selectedIconPath": "/images/recharge_selected.png",
}, {
"text": "车队",
"iconPath": "/images/market.png",
"selectedIconPath": "/images/market_selected.png",
}
]
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.tabComponent = this.selectComponent('#tabComponent');
let selectedColor = this.data.selectedColor;
let color = this.data.color;
this.tabComponent.setData({
selectedColor: selectedColor,
color:color
})
console.log(this.tabComponent.data.tabbarIndex)
},
//获取组件传递出来的数据
tabChange:function(e){
let index = e.detail.tabbarIndex;
this.setData({
tabbarIndex:index
})
console.log(e.detail.tabbarIndex)
}
})
最终效果如图所示:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍Vuex的作用,很多新手对于Vuex的原理、使用以及其他概念不是很了解,下面小编就带大家深入的来接Vuex,感兴趣的朋友就继续往下看吧,希望大家阅读完这篇文章能有所收获。
这篇文章主要介绍了前端的状态管理,续上篇文章内容,今天将从 Redux 入手逐渐拓展,需要的小伙伴可以参考一下哟
Array.from可以从类似的数组或可迭代对象中创建一个新的、浅拷贝的数组实例。Array.from接收三个参数:必须选择类似数组的对象、加工函数、this作用域。
本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于number类型的相关知识,包括了number类型的常见误区背后原理以及解决方法等内容,下面一起来看一下,希望对大家有帮助。
去除HTML中没有ID元素的方法:1、使用“$("父元素").children(":not([id])")”语句获取到需要去除的所有没有ID的元素;2、使用remove()方法删除获取到的元素即可,语法“没有ID的元素.remove()”。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008