微信小程序自定义tabbar组件
本文实例为大家分享了微信小程序自定义tabbar组件的具体代码,供大家参考,具体内容如下
由于项目需求,必须自己写组件:
第一步:在App.json中配置tabBar,自定也组件也必须配置,"custom": true,list里配置所有的tabbar页面。
"tabBar":{
"custom":true,
"color":"red",
"selectedColor":"#3b81d7",
"backgroundColor":"#000000",
"list":[{
"pagePath":"pages/Role/InsureIndex/index",
"text":"首页"
},{
"pagePath":"pages/Role/MineIndex/index",
"text":"首页"
},{
"pagePath":"pages/index/userInfo/userInfo",
"text":"我的"
}]
},
第二步:在pages的同级目录新建组件,文件夹名字:custom-tab-bar,自定义组件文件名为index。组件代码如下,应该都能看懂。
index.js
Component({
properties:{},
data:{
indexImg:"../static/images/tabBar/tab_icon_home_nor@2x.png",
indexSelectImg:"../static/images/tabBar/tab_icon_home_sel@2x.png",
aboutUsImg:"../static/images/tabBar/tab_icon_user_nor@2x.png",
aboutUsSelectImg:"../static/images/tabBar/tab_icon_user_sel@2x.png",
_tabbat:null,
iPhoneX:false,
urls:['/pages/Role/InsureIndex/index',
'/pages/index/userInfo/userInfo'
]
},
attached(){
varself=this
//此为业务代码,可不看
wx.getStorage({
key:'userInfo',
success:function(res){
const{
userRoleCode
}=res.data
if(userRoleCode=='50'||userRoleCode=='70'){
self.setData({
["urls[0]"]:'/pages/Role/MineIndex/index'
})
}elseif(userRoleCode=='30'||userRoleCode=='35'||userRoleCode=='40'){
self.setData({
["urls[0]"]:'/pages/Role/InsureIndex/index'
})
}
}
})
wx.getSystemInfo({
success(res){
console.log(res.model)
if(res.model.indexOf('iPhoneX')>=0){
self.setData({
iPhoneX:true
})
}
}
})
},
/**
*组件的方法列表
*/
methods:{
switchTap:function(e){
varself=this
varindex=e.currentTarget.dataset.index;
varurls=self.data.urls
wx.switchTab({
url:urls[index],
})
}
}
})
index.wxml
首页