小程序自定義tabbar導(dǎo)航欄及動(dòng)態(tài)控制tabbar功能實(shí)現(xiàn)方法(uniapp)
uniapp開發(fā)小程序,不同角色/已登錄未登錄,都有不一樣的底部導(dǎo)航欄,這些情況下就需要自行定義tabbar,從而實(shí)現(xiàn)動(dòng)態(tài)tabbar的實(shí)現(xiàn)。


1.首先我們需要在pages.json配置tabbar
我這里并沒有開啟custom(自定義),不開啟的話,他在頁面是有占位的,那就需要在頁面進(jìn)行隱藏,后面會(huì)講到;
這里是直接給一個(gè)路徑就可以,用于后期使用uni.switchTab(OBJECT)進(jìn)行跳轉(zhuǎn)
"tabBar": {
// "custom": true,
"list": [{
"pagePath": "pages/home/index"
},
{
"pagePath": "pages/personal/index"
},
{
"pagePath": "pages/personal/notMemberIndex"
}
]
}
2.我們需要配置tabbar列表,根據(jù)角色的不同設(shè)置不同的tabbar列表數(shù)據(jù)
我是登錄的用戶跟未登錄的用戶是不同的tabbar的一個(gè)顯示;
重點(diǎn): ?。?這里的text,pagePath,iconPath, selectedIconPath,這四個(gè)命名必須跟pages.json里面tabBar配置的原始命名一致,否則會(huì)出問題??!

// 已登錄
const member = [{
"text": "首頁",
"pagePath": "/pages/home/index",
"iconPath": require("@/static/home.png"),
"selectedIconPath": require("@/static/homeSelect.png")
},
{
"text": "個(gè)人中心",
"pagePath": "/pages/personal/index",
"iconPath": require("@/static/personal.png"),
"selectedIconPath": require("@/static/personalSelect.png")
}
]
// 未登錄
const notMember = [{
"text": "山姆會(huì)員商城",
"pagePath": "/pages/home/index",
"iconPath": require("@/static/home.png"),
"selectedIconPath": require("@/static/homeSelect.png")
},
{
"text": "成為會(huì)員",
"pagePath": "/pages/personal/notMemberIndex",
"iconPath": require("@/static/notMember.png"),
"selectedIconPath": require("@/static/notMemberSelect.png")
}
]
export default {
member,
notMember
}
3.使用vuex對(duì)tabBar列表數(shù)據(jù)進(jìn)行一個(gè)存儲(chǔ)賦值

tabBar.js
對(duì)數(shù)據(jù)進(jìn)行一個(gè)存儲(chǔ),賦值
import tarBarUserType from '@/utils/tabBar.js';
const tabBar = {
state: {
// 判斷是否已登錄(member/notMember)
isMemberType: '',
// tabbar列表數(shù)據(jù)
tabBarList: []
},
mutations: {
setType(state, isMemberType) {
state.isMemberType = isMemberType;
state.tabBarList = tarBarUserType[isMemberType];
}
}
}
export default tabBar;
getters.js
獲取存儲(chǔ)在vuex的內(nèi)容
const getters = {
tabBarList: state => state.tabBar.tabBarList,
isMemberType: state => state.tabBar.isMemberType,
}
export default getters4.需要自行封裝一個(gè)tabbar組件
附上我自己簡單封裝的一個(gè)組件
<template>
<view class="tab-bar">
<view class="content">
<view class="one-tab" v-for="(item, index) in tabBarList" :key="index" @click="selectTabBar(item.pagePath)">
<view>
<view class="tab-img">
<image v-if="routePath === item.pagePath" class="img" :src="item.selectedIconPath"></image>
<image v-else class="img" :src="item.iconPath"></image>
</view>
</view>
<view class="tit">{{ item.text }}</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
// 底部導(dǎo)航欄數(shù)據(jù)
tabBarList: {
type: Array,
required: true
},
// 當(dāng)前頁面路徑
routePath: {
type: String,
required: true
}
},
data() {
return {};
},
methods: {
selectTabBar(path) {
this.$emit('onTabBar', path)
}
}
};
</script>
<style lang="scss">
.tab-bar {
position: fixed;
bottom: 0;
left: 0;
width: 100vw;
padding: 20rpx;
padding-bottom: calc(10rpx + constant(safe-area-inset-bottom));
padding-bottom: calc(10rpx + env(safe-area-inset-bottom));
background-color: #fff;
.content {
display: flex;
.one-tab {
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
.tab-img {
width: 50rpx;
height: 50rpx;
.img {
width: 100%;
height: 100%;
}
}
.tit {
font-size: $font-size-base;
transform: scale(0.7);
}
}
}
}
</style>5.在存在tabbar的頁面中都需要引入組件,并傳相關(guān)數(shù)據(jù)
6.在這些頁面需要用到getters.js獲取拿到這些數(shù)據(jù)
在存在tabbar頁面的都需要使用計(jì)算屬性獲取tabbar數(shù)據(jù)!!
import { mapGetters } from 'vuex';
computed: {
// 這里的tabBarList就是數(shù)據(jù)源,直接使用傳值
...mapGetters(['tabBarList'])
},
7.判斷是否登錄
我這里是有個(gè)登錄頁面的,在登錄頁面的時(shí)候進(jìn)行一個(gè)分配tabbar列表配置的一個(gè)操作(具體看個(gè)人業(yè)務(wù)邏輯)
// 存儲(chǔ)用戶信息
setUserWxInfo(userInfo);
// 調(diào)用上文vuex,member就是已登錄的標(biāo)識(shí)
that.$store.commit('setType', 'member');

8.隱藏tabBar
上文提到我是在pages.json中配置tabBar的時(shí)候是沒有開啟custom自定義模式的,所以說他在頁面是有占位的
在APP.vue, 以及所用到tabbar的頁面,在onShow中調(diào)用 uni.hideTabBar({}); 對(duì)原始tabbar進(jìn)行一個(gè)隱藏
9.在APP.vue中 對(duì)用戶狀態(tài)進(jìn)行判斷
在APP.vue中onShow里進(jìn)行用戶判斷,如果已登錄就傳入member,否則傳入notMember
onShow() {
uni.hideTabBar({});
if (getUserWxInfo('user_info')) {
this.$store.commit('setType', 'member');
} else {
this.$store.commit('setType', 'notMember');
}
}
完結(jié)~
大致就可以完成動(dòng)態(tài)自定義tabbar這項(xiàng)需求了!
總結(jié)
到此這篇關(guān)于小程序自定義tabbar導(dǎo)航欄及動(dòng)態(tài)控制tabbar功能實(shí)現(xiàn)方法(uniapp)的文章就介紹到這了,更多相關(guān)小程序自定義tabbar導(dǎo)航欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp小程序底部tabbar圖標(biāo)大小設(shè)置辦法
- uniapp原生tabbar設(shè)置并添加數(shù)字角標(biāo)或小紅點(diǎn)提示功能
- uniapp小程序自定義tabbar以及初次加載閃屏解決方法
- uniapp自定義tabbar的方法(支持中間凸起、角標(biāo)、動(dòng)態(tài)隱藏tab和全端適用)
- uniapp小程序配置tabbar底部導(dǎo)航欄實(shí)戰(zhàn)指南
- uniapp微信小程序底部動(dòng)態(tài)tabBar的解決方案(自定義tabBar導(dǎo)航)
- uniapp如何使用uv-popup彈出框隱藏底部導(dǎo)航tabbar
相關(guān)文章
使用echarts實(shí)現(xiàn)3d柱狀圖+折線圖
這篇文章主要為大家詳細(xì)介紹了如何使用echarts實(shí)現(xiàn)3d柱狀圖和折線圖,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以了解一下2024-12-12
處理文本部分內(nèi)容的TextRange對(duì)象應(yīng)用實(shí)例
TextRange是用來表現(xiàn)HTML元素中文字的對(duì)象,是一個(gè)用于處理JavaScript對(duì)象文本部分內(nèi)容的一個(gè)對(duì)象2014-07-07
微信公眾號(hào)網(wǎng)頁分享功能開發(fā)的示例代碼
這篇文章主要介紹了微信公眾號(hào)網(wǎng)頁分享功能開發(fā)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

