微信小程序如何根據(jù)不同用戶切換不同TabBar(簡單易懂!)
現(xiàn)有需求:
小程序用戶有三種身份(公眾、運(yùn)維人員、領(lǐng)導(dǎo)),根據(jù)不同用戶身份顯示不同的tabbar
眾所周知微信小程序全局文件app.json里面的"tabBar"里面的list只能放置2-5個,要想實(shí)現(xiàn)3個tabbar,必須得復(fù)用tabbar,三種身份都需要個人中心,剩下的是長列表(兩個),表單,圖表 剛好是5個,廢話少說,上代碼
代碼有點(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": "我要報修", "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": "個人中心", "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": "故障報表", "iconPath": "/images/tabbar/chart1.png", "selectedIconPath": "/images/tabbar/chart2.png" } ] }, "sitemapLocation": "sitemap.json" }
可以看到全局app.json里面放了5個不同的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: "我要報修" }, { pagePath: "/pages/Users/myMalfunction/myMalfunction", iconPath: "/images/tabbar/myweixiu1.png", selectedIconPath: "/images/tabbar/myweixiu2.png", text: "我的故障" }, { pagePath: "/pages/logs/logs", text: "個人中心", 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: "個人中心", 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: "我要報修" }, { pagePath: "/pages/charts/charts", iconPath: "/images/tabbar/chart1.png", selectedIconPath: "/images/tabbar/chart2.png", text: "故障報表" }, { pagePath: "/pages/logs/logs", text: "個人中心", 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中定義一個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)文章
JS打印gridview實(shí)現(xiàn)原理及代碼
打印gridview對于一些童鞋們真的是很陌生啊,不過沒有關(guān)系,因?yàn)楸疚牡某霈F(xiàn),或讓你茅塞頓開,好了話不多說,感興趣的朋友可以了解下,或許對你學(xué)習(xí)js高級知識有所幫助2013-02-02使用Promise和JavaScript有效處理1000個請求的方法
在現(xiàn)代Web開發(fā)中,處理高并發(fā)請求是一個常見的挑戰(zhàn),當(dāng)我們需要從服務(wù)器獲取大量數(shù)據(jù)或執(zhí)行多個異步任務(wù)時,如何有效地管理請求的并發(fā)性和性能變得至關(guān)重要,本文將介紹如何使用Promise和JavaScript來管理高并發(fā)請求,需要的朋友可以參考下2023-09-09canvas實(shí)現(xiàn)圖片根據(jù)滑塊放大縮小效果
本文主要介紹了canvas實(shí)現(xiàn)圖片根據(jù)滑塊放大縮小效果的實(shí)例,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02微信小程序button標(biāo)簽open-type屬性原理解析
這篇文章主要介紹了微信小程序button標(biāo)簽open-type屬性原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01javascript實(shí)現(xiàn)簡單小鋼琴有聲彈奏效果
用HTML5+javascript實(shí)現(xiàn)的小鋼琴,按下鋼琴鍵上的相應(yīng)字母用或用鼠標(biāo)點(diǎn)擊鋼琴鍵發(fā)聲,javascript代碼包含了對鼠標(biāo)按下、移動和松開,以及鍵盤按下的事件監(jiān)聽2024-02-02微信小程序中的video視頻實(shí)現(xiàn) 自定義播放按鈕、封面圖、視頻封面上文案
這篇文章主要介紹了微信小程序中的video視頻實(shí)現(xiàn) 自定義播放按鈕、封面圖、視頻封面上文案,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01ASP中進(jìn)行HTML數(shù)據(jù)及JS數(shù)據(jù)編碼函數(shù)
在有些時候我們無法控制亂碼的出現(xiàn), 比如發(fā)送郵件的時候有些郵件顯示亂碼, 比如Ajax返回數(shù)據(jù)總是亂碼. 怎么辦?2009-11-11JavaScript的new date等日期函數(shù)在safari中遇到的坑
safari中對于JavaScript的new Date函數(shù)的支持有一個比較奇怪的問題,帶著這個奇怪的問題我們通過本文一起學(xué)習(xí)吧2016-10-10