
?1.權(quán)限一: 擁有的底部欄如圖
1.2 權(quán)限二: 擁有的底部欄 如圖
1.3?定義全局屬性用于存儲底部的tabbar渲染
一:??首先在全局文件App.json?配置tabbar (最少2個?最多5個),并且配置頁面
{
"pages": [
"pages/login/index",
"pages/zy/index",
"pages/myInfo/index",
"pages/onlyOne/index",
"pages/friend/index",
"pages/setting/index"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "微信登錄",
"navigationBarTextStyle": "black"
},
"tabBar": {
"custom": true,
"color": "#666666",
"selectedColor": "#FF5F15",
"backgroundColor": "#ffffff",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/zy/index",
"text": "主頁"
},
{
"pagePath": "pages/myInfo/index",
"text": "我的信息"
},
{
"pagePath": "pages/onlyOne/index",
"text": "個人中心"
},
{
"pagePath": "pages/friend/index",
"text": "朋友信息"
},
{
"pagePath": "pages/setting/index",
"text": "手機設(shè)置"
}
]
},
"sitemapLocation": "sitemap.json"
}
二:login?頁面
1.wxml:
<view class="title">
歡迎登錄
</view>
<!-- <button open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar" type="primary" size="mini">微信授權(quán)登錄</button> -->
<view>
<button bindtap="login" data-type="1" type="primary" size="mini">權(quán)限1</button>
</view>
<view>
<button bindtap="login" data-type="2" type="primary" size="mini">權(quán)限2</button>
</view>
2. js
// pages/login/index.js
const app = getApp().globalData //獲取并設(shè)置tabbar
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
},
login(e){
const type = e.target.dataset.type
if(type == 1){ //用戶權(quán)限
app.routerList = [
{
name:"主頁",
icon:"home-o",
url:"/pages/zy/index",
},
{
name:"我的信息",
icon:"chat-o",
url:"/pages/myInfo/index",
},
{
name:"個人中心",
icon:"https://b.yzcdn.cn/vant/icon-demo-1126.png",
url:"/pages/onlyOne/index",
}
]
wx.reLaunch({
url: '/pages/zy/index',
})
}else{
app.routerList = [
{
name:"朋友信息",
icon:"friends-o",
url:"/pages/friend/index",
},
{
name:"手機設(shè)置",
icon:"setting-o",
url:"/pages/setting/index",
},
{
name:"個人中心",
icon:"https://b.yzcdn.cn/vant/icon-demo-1126.png",
url:"/pages/onlyOne/index",
}
]
wx.reLaunch({
url: '/pages/friend/index',
})
}
},
})
如此一來便有了?登錄后的路由信息
緊接著 第二步:?創(chuàng)建custom-tab-bar?文件夾?與pages?同級(如圖:)
第三步:
custom-tab-bar?中編寫代碼
1.在index.wxml 中編寫:
<van-tabbar active="{{ active }}" bind:change="onChange">
<van-tabbar-item bindtap="loadPage" data-url="{{item.url}}" wx:for="{{routerList}}" wx:key="index" icon="{{item.icon}}">
{{item.name}}
</van-tabbar-item>
</van-tabbar>
2.在index.json 中編寫:注意此處用的vant ui 庫 如沒下載清先npm下載后使用
https://vant-contrib.gitee.io/vant-weapp/#/tabbar#api
{
"component": true,
"usingComponents": {
"van-tabbar": "@vant/weapp/tabbar/index",
"van-tabbar-item": "@vant/weapp/tabbar-item/index",
"van-icon": "@vant/weapp/icon/index"
}
}
3.在index.js中:
Component({
/**
* 組件的屬性列表
*/
properties: {
active:{ //對外提供當(dāng)前選中的項 可以直接在每個頁面中引入 以避免 tabbar顯示與點擊不同步的現(xiàn)象
type:Number,
value:0
}
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
routerList:[]
},
lifetimes:{
attached(){
this.setData({ routerList: getApp().globalData.routerList}); //獲取路由
console.log(this.data.routerList)
}
},
/**
* 組件的方法列表
*/
methods: {
onChange(event) {
// event.detail 的值為當(dāng)前選中項的索引
this.setData({ active: event.detail });
},
loadPage(event){
wx.switchTab({
url: event.target.dataset.url,
})
},
}
})
第四步:
1.1 在每個頁面的onShow?生命周期函數(shù)中加上這句代碼:
如果不加會導(dǎo)致tabbar顯示與點擊不同步的現(xiàn)象
onShow() {
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
console.log(this.getTabBar())
this.getTabBar().setData({
active: 1 //這里的active的值根據(jù)你的routerList 順序一致
})
}
},
1.2?如果不想加? 也可以在你不想加上面那句代碼的頁面?中?配置json
如圖:
?文章來源:http://www.zghlxwxcb.cn/news/detail-431844.html
?wxml:active?直接設(shè)置為0即可文章來源地址http://www.zghlxwxcb.cn/news/detail-431844.html
到了這里,關(guān)于微信小程序 根據(jù)登錄后的角色權(quán)限 渲染底部tabbar--自定義渲染tabbar的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!