微信小程序如何根據(jù)不同用戶切換不同TabBar(簡單易懂!)
現(xiàn)有需求:
小程序用戶有三種身份(公眾、運(yùn)維人員、領(lǐng)導(dǎo)),根據(jù)不同用戶身份顯示不同的tabbar
眾所周知微信小程序全局文件app.json里面的"tabBar"里面的list只能放置2-5個(gè),要想實(shí)現(xiàn)3個(gè)tabbar,必須得復(fù)用tabbar,三種身份都需要個(gè)人中心,剩下的是長列表(兩個(gè)),表單,圖表 剛好是5個(gè),廢話少說,上代碼
代碼有點(diǎn)長,建議仔細(xì)看一下
1全局.app.json
tabbar里面的sustom要設(shè)置為true
"custom": true,
{
"pages": [
xxxxxx:xxxxxx
],
"window": {
xxxxxxxxx
},
"tabBar": {
"custom": true,
"color": "#7A7E83",
"selectedColor": "#d81e06",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/Users/myrepaire/myrepaire",
"text": "我要報(bào)修",
"iconPath": "/images/tabbar/weixiu1.png",
"selectedIconPath": "/images/tabbar/weixiu2.png"
},
{
"pagePath": "pages/Users/myMalfunction/myMalfunction",
"text": "我的故障",
"iconPath": "/images/tabbar/myweixiu1.png",
"selectedIconPath": "/images/tabbar/myweixiu2.png"
},
{
"pagePath": "pages/logs/logs",
"text": "個(gè)人中心",
"iconPath": "/images/tabbar/user1.png",
"selectedIconPath": "/images/tabbar/user2.png"
},
{
"pagePath": "pages/weixiu/myweixiu/myweixiu",
"text": "我的維修",
"iconPath": "/images/tabbar/myweixiu1.png",
"selectedIconPath": "/images/tabbar/myweixiu1.png"
},
{
"pagePath": "pages/charts/charts",
"text": "故障報(bào)表",
"iconPath": "/images/tabbar/chart1.png",
"selectedIconPath": "/images/tabbar/chart2.png"
}
]
},
"sitemapLocation": "sitemap.json"
}
可以看到全局app.json里面放了5個(gè)不同的tabbar路徑
2.自定義custom-tab-bar

index.js
Component({
data: {
selected: 0,
color: "#000000",
roleId: '',
selectedColor: "#1396DB",
allList: [{
list1: [{
pagePath: "/pages/Users/myrepaire/myrepaire",
iconPath: "/images/tabbar/weixiu1.png",
selectedIconPath: "/images/tabbar/weixiu2.png",
text: "我要報(bào)修"
}, {
pagePath: "/pages/Users/myMalfunction/myMalfunction",
iconPath: "/images/tabbar/myweixiu1.png",
selectedIconPath: "/images/tabbar/myweixiu2.png",
text: "我的故障"
}, {
pagePath: "/pages/logs/logs",
text: "個(gè)人中心",
iconPath: "/images/tabbar/user1.png",
selectedIconPath: "/images/tabbar/user2.png"
}],
list2: [{
pagePath: "/pages/weixiu/myweixiu/myweixiu",
iconPath: "/images/tabbar/weixiu1.png",
selectedIconPath: "/images/tabbar/weixiu2.png",
text: "我要維修"
}, {
pagePath: "/pages/Users/myMalfunction/myMalfunction",
iconPath: "/images/tabbar/myweixiu1.png",
selectedIconPath: "/images/tabbar/myweixiu2.png",
text: "我的維修"
}, {
pagePath: "/pages/logs/logs",
text: "個(gè)人中心",
iconPath: "/images/tabbar/user1.png",
selectedIconPath: "/images/tabbar/user2.png"
}],
list3: [{
pagePath: "/pages/Users/myrepaire/myrepaire",
iconPath: "/images/tabbar/weixiu1.png",
selectedIconPath: "/images/tabbar/weixiu2.png",
text: "我要報(bào)修"
}, {
pagePath: "/pages/charts/charts",
iconPath: "/images/tabbar/chart1.png",
selectedIconPath: "/images/tabbar/chart2.png",
text: "故障報(bào)表"
}, {
pagePath: "/pages/logs/logs",
text: "個(gè)人中心",
iconPath: "/images/tabbar/user1.png",
selectedIconPath: "/images/tabbar/user2.png"
}]
}],
list: []
},
attached() {
const roleId = wx.getStorageSync('statu')
if (roleId == 20) {
this.setData({
list: this.data.allList[0].list1
})
}else if(roleId==5){
this.setData({
list: this.data.allList[0].list3
})
}else if(roleId==102){
this.setData({
list: this.data.allList[0].list2
})
}
},
methods: {
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({ url })
this.setData({
selected: data.index
})
}
},
})分析:
- 首先,小程序tabbar只識別list里面的東西
- 先在data中定義一個(gè)list和allList數(shù)組,把三重身份用戶的list分別定義為list1,list2,list3,放入allList
const roleId = wx.getStorageSync('statu')
獲取用戶信息存到緩存中,根據(jù)不同和的roleId來判斷是什么身份,this.setData({ list: this.data.allList[0].list2 })
根據(jù)身份把a(bǔ)llList里面的子數(shù)組賦值給系統(tǒng)默認(rèn)識別的`list``- switchTab的作用根據(jù)不同的路徑切換tabbar下標(biāo)
switchTab(e) {
const data = e.currentTarget.dataset
const url = data.path
wx.switchTab({ url })
this.setData({
selected: data.index
})
}
index.json
{
"usingComponents": {}
}
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="switchTab">
<cover-image class="cover-image" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
<cover-view class="cover-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
</cover-view>
</cover-view>index.wxss
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 48px;
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: 44rpx;
height: 44rpx;
}
.tab-bar-item .cover-view {
margin-top: 8rpx;
font-size: 24rpx;
}
最后,在tabbar里面設(shè)置過 pagePath的路徑文件下的 xxx.js的onshow:function(){}里面設(shè)置
或者說凡是用到tabbar組件的頁面對應(yīng)的xx.js里的onshow:function(){}都要按照以下進(jìn)行設(shè)置
不然會出現(xiàn)tabbar顯示與點(diǎn)擊不同步的現(xiàn)象
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 0
})
}
},
//selected: 0就是選中的tabbar下標(biāo),根據(jù)不同頁面顯示不同tabbar下標(biāo)結(jié)果展示
1.普通用戶

2.運(yùn)維人員

3.領(lǐng)導(dǎo)

可以看到根據(jù)用戶信息里的roleId成功的切換了不同的tabbar
總結(jié)
到此這篇關(guān)于微信小程序如何根據(jù)不同用戶切換不同TabBar的文章就介紹到這了,更多相關(guān)小程序用戶切換不同TabBar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)打開鏈接頁面的方式匯總
這篇文章主要介紹了JavaScript實(shí)現(xiàn)打開鏈接頁面的方式,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
如何高效率去掉js數(shù)組中的重復(fù)項(xiàng)
如何高效率去掉js數(shù)組中的重復(fù)項(xiàng)呢?下面小編就為大家?guī)硪黄咝嗜サ鬸s數(shù)組中重復(fù)項(xiàng)的實(shí)現(xiàn)方法。希望對大家有所幫助。一起跟隨小編過來看看吧2016-04-04
利用Javascript實(shí)現(xiàn)一套自定義事件機(jī)制
隨著web技術(shù)發(fā)展,使用JavaScript自定義對象愈發(fā)頻繁,讓自己創(chuàng)建的對象也有事件機(jī)制,通過事件對外通信,能夠極大提高開發(fā)效率。下面這篇文章主要給大家介紹了關(guān)于利用Javascript實(shí)現(xiàn)一套自定義事件機(jī)制的相關(guān)資料,需要的朋友可以參考下。2017-12-12
BootstrapTable與KnockoutJS相結(jié)合實(shí)現(xiàn)增刪改查功能【二】
這篇文章主要介紹了BootstrapTable與KnockoutJS相結(jié)合實(shí)現(xiàn)增刪改查功能【二】的相關(guān)資料,非常具有參考價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05

