您好,登錄后才能下訂單哦!
今天小編給大家分享一下微信小程序怎么根據不同用戶切換不同TabBar的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
小程序用戶有三種身份(公眾、運維人員、領導),根據不同用戶身份顯示不同的tabbar
眾所周知微信小程序全局文件app.json里面的"tabBar"里面的list只能放置2-5個,要想實現3個tabbar,必須得復用tabbar,三種身份都需要個人中心,剩下的是長列表(兩個),表單,圖表 剛好是5個,廢話少說,上代碼
tabbar里面的sustom要設置為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路徑
(詳見微信官方文檔)
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數組,把三重身份用戶的list分別定義為list1,list2,list3,放入allList
const roleId = wx.getStorageSync('statu')
獲取用戶信息存到緩存中,根據不同和的roleId來判斷是什么身份,
this.setData({ list: this.data.allList[0].list2 })
根據身份把allList里面的子數組賦值給系統默認識別的`list``
switchTab的作用根據不同的路徑切換tabbar下標
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" >{{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里面設置過 pagePath的路徑文件下的 xxx.js的onshow:function(){}里面設置
或者說凡是用到tabbar組件的頁面對應的xx.js里的onshow:function(){}都要按照以下進行設置
不然會出現tabbar顯示與點擊不同步的現象
/** * 生命周期函數--監聽頁面顯示 */ onShow: function () { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 }) } }, //selected: 0就是選中的tabbar下標,根據不同頁面顯示不同tabbar下標
1.普通用戶
2.運維人員
3.領導
可以看到根據用戶信息里的roleId成功的切換了不同的tabbar
以上就是“微信小程序怎么根據不同用戶切換不同TabBar”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。