赞
踩
小程序中我们可能需要根据不同的权限展示不同的tabbar,比如你是会员的话,那么你的底部就展示一个VIP的tabbar,如果是普通用户的话就把这个VIP的tabbar隐藏掉。
首先我们先实现一个自定义的tabbar:
// custom-tab-bar中的js const app = getApp(); Component({ data: { active:'0', list: [{ url:'/pages/index/index', text: "首页", name:0, icon:'home-o' }, { url:'/pages/logs/logs', text: "日志", name:1, icon:'search' }], vipList: [{ url:'/pages/index/index', text: "首页", name:0, icon:'home-o' }, { url:'/pages/logs/logs', text: "日志", name:1, icon:'search' },{ url:'/pages/vip/vip', text: "VIP", name:2, icon:'friends-o' }], noVipList: [{ url:'/pages/index/index', text: "首页", name:0, icon:'home-o' }, { url:'/pages/logs/logs', text: "日志", name:1, icon:'search' }] }, attached() { }, methods: { init() { const page = getCurrentPages().pop(); this.setData({ active: this.data.list.findIndex(item => item.url === `/${page.route}`) }); }, // 这是用了切换不同的权限的,做个判断切换不同的权限 checkPermission() { if (app.globalData.userRank == 0) { console.log(app.globalData.userRank) this.setData({ list: this.data.noVipList, }) } if (app.globalData.userRank == 1) { console.log(app.globalData.userRank) this.setData({ list: this.data.vipList, }) } }, onChange(event) { console.log(event) this.setData({ active: event.detail }); wx.switchTab({ url: this.data.list[event.detail].url }); }, } })
5.在每个页面的文件夹中去调用init, this.getTabBar().init() 每个页面都要调用,切换权限就是下面的那个onChange了
// 页面中的js const app = getApp(); Page({ data: { radio: 109, chepaihao:'', checked:false }, onClick(event) { const { name } = event.currentTarget.dataset; console.log(name) this.setData({ chepaihao: name, }); }, onChange({ detail }) { let _this = this if(detail) { app.globalData.userRank = 1 console.log( app.globalData.userRank) _this.getTabBar().checkPermission() }else { app.globalData.userRank = 0 _this.getTabBar().checkPermission() } this.setData({ checked: detail }); }, onShow() { console.log( this.getTabBar()) this.getTabBar().init() } });
总结:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。