在開發uniapp小程序時,底部導航欄(tabbar)是一個非常常見的功能需求。它可以幫助用戶快速切換不同的頁面,提升用戶體驗。本文將詳細介紹如何在uniapp中配置tabbar底部導航欄,包括基本配置、自定義樣式、動態切換、以及常見問題的解決方案。
首先,我們需要創建幾個頁面,這些頁面將作為tabbar的導航項。假設我們創建了以下四個頁面:
pages/index/index
:首頁pages/category/category
:分類頁pages/cart/cart
:購物車頁pages/user/user
:用戶中心頁pages.json
在uniapp中,pages.json
文件用于配置頁面的路由和窗口表現。我們需要在這個文件中配置tabbar。
{
"pages": [
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "首頁"
}
},
{
"path": "pages/category/category",
"style": {
"navigationBarTitleText": "分類"
}
},
{
"path": "pages/cart/cart",
"style": {
"navigationBarTitleText": "購物車"
}
},
{
"path": "pages/user/user",
"style": {
"navigationBarTitleText": "用戶中心"
}
}
],
"tabBar": {
"color": "#999999",
"selectedColor": "#007AFF",
"backgroundColor": "#ffffff",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/index/index",
"text": "首頁",
"iconPath": "static/tabbar/home.png",
"selectedIconPath": "static/tabbar/home-active.png"
},
{
"pagePath": "pages/category/category",
"text": "分類",
"iconPath": "static/tabbar/category.png",
"selectedIconPath": "static/tabbar/category-active.png"
},
{
"pagePath": "pages/cart/cart",
"text": "購物車",
"iconPath": "static/tabbar/cart.png",
"selectedIconPath": "static/tabbar/cart-active.png"
},
{
"pagePath": "pages/user/user",
"text": "我的",
"iconPath": "static/tabbar/user.png",
"selectedIconPath": "static/tabbar/user-active.png"
}
]
}
}
color
:未選中時的文字顏色。selectedColor
:選中時的文字顏色。backgroundColor
:tabbar的背景顏色。borderStyle
:tabbar上邊框的顏色,可選值為black
或white
。list
:tabbar的導航項列表,每個導航項包含以下屬性:
pagePath
:頁面路徑。text
:導航項的文字。iconPath
:未選中時的圖標路徑。selectedIconPath
:選中時的圖標路徑。默認情況下,uniapp的tabbar高度為50px。如果需要修改tabbar的高度,可以通過以下方式實現:
{
"tabBar": {
"height": "60px",
"list": [
// 導航項配置
]
}
}
uniapp默認的tabbar圖標大小為24px。如果需要修改圖標大小,可以通過以下方式實現:
{
"tabBar": {
"iconWidth": "30px",
"iconHeight": "30px",
"list": [
// 導航項配置
]
}
}
如果需要更復雜的樣式定制,可以通過在pages.json
中配置custom
屬性來實現:
{
"tabBar": {
"custom": true,
"list": [
// 導航項配置
]
}
}
然后在App.vue
中編寫自定義的tabbar組件:
<template>
<view class="custom-tabbar">
<view class="tabbar-item" v-for="(item, index) in tabbarList" :key="index" @click="switchTab(index)">
<image :src="currentIndex === index ? item.selectedIconPath : item.iconPath" class="tabbar-icon"></image>
<text :class="['tabbar-text', currentIndex === index ? 'active' : '']">{{ item.text }}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
tabbarList: [
{
pagePath: "/pages/index/index",
text: "首頁",
iconPath: "/static/tabbar/home.png",
selectedIconPath: "/static/tabbar/home-active.png"
},
{
pagePath: "/pages/category/category",
text: "分類",
iconPath: "/static/tabbar/category.png",
selectedIconPath: "/static/tabbar/category-active.png"
},
{
pagePath: "/pages/cart/cart",
text: "購物車",
iconPath: "/static/tabbar/cart.png",
selectedIconPath: "/static/tabbar/cart-active.png"
},
{
pagePath: "/pages/user/user",
text: "我的",
iconPath: "/static/tabbar/user.png",
selectedIconPath: "/static/tabbar/user-active.png"
}
]
};
},
methods: {
switchTab(index) {
this.currentIndex = index;
uni.switchTab({
url: this.tabbarList[index].pagePath
});
}
}
};
</script>
<style>
.custom-tabbar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 60px;
display: flex;
justify-content: space-around;
align-items: center;
background-color: #ffffff;
border-top: 1px solid #e5e5e5;
}
.tabbar-item {
display: flex;
flex-direction: column;
align-items: center;
}
.tabbar-icon {
width: 24px;
height: 24px;
}
.tabbar-text {
font-size: 12px;
color: #999999;
}
.tabbar-text.active {
color: #007AFF;
}
</style>
在某些場景下,我們可能需要動態切換tabbar的顯示內容。例如,用戶登錄后顯示不同的tabbar項??梢酝ㄟ^以下方式實現:
pages.json
uniapp不支持直接動態修改pages.json
文件,但可以通過條件渲染來實現動態切換tabbar的效果。
<template>
<view class="custom-tabbar">
<view class="tabbar-item" v-for="(item, index) in tabbarList" :key="index" @click="switchTab(index)">
<image :src="currentIndex === index ? item.selectedIconPath : item.iconPath" class="tabbar-icon"></image>
<text :class="['tabbar-text', currentIndex === index ? 'active' : '']">{{ item.text }}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
tabbarList: []
};
},
created() {
this.updateTabbar();
},
methods: {
updateTabbar() {
const isLogin = uni.getStorageSync('isLogin');
this.tabbarList = isLogin ? [
{
pagePath: "/pages/index/index",
text: "首頁",
iconPath: "/static/tabbar/home.png",
selectedIconPath: "/static/tabbar/home-active.png"
},
{
pagePath: "/pages/category/category",
text: "分類",
iconPath: "/static/tabbar/category.png",
selectedIconPath: "/static/tabbar/category-active.png"
},
{
pagePath: "/pages/cart/cart",
text: "購物車",
iconPath: "/static/tabbar/cart.png",
selectedIconPath: "/static/tabbar/cart-active.png"
},
{
pagePath: "/pages/user/user",
text: "我的",
iconPath: "/static/tabbar/user.png",
selectedIconPath: "/static/tabbar/user-active.png"
}
] : [
{
pagePath: "/pages/index/index",
text: "首頁",
iconPath: "/static/tabbar/home.png",
selectedIconPath: "/static/tabbar/home-active.png"
},
{
pagePath: "/pages/category/category",
text: "分類",
iconPath: "/static/tabbar/category.png",
selectedIconPath: "/static/tabbar/category-active.png"
},
{
pagePath: "/pages/user/user",
text: "我的",
iconPath: "/static/tabbar/user.png",
selectedIconPath: "/static/tabbar/user-active.png"
}
];
},
switchTab(index) {
this.currentIndex = index;
uni.switchTab({
url: this.tabbarList[index].pagePath
});
}
}
};
</script>
如果tabbar沒有顯示,可能是以下原因導致的:
pages.json
中的tabBar
配置有誤。如果tabbar圖標沒有顯示,可能是以下原因導致的:
如果tabbar切換時頁面沒有刷新,可能是以下原因導致的:
可以通過在onShow
生命周期鉤子中處理頁面刷新邏輯:
export default {
onShow() {
// 頁面顯示時刷新數據
}
};
通過本文的介紹,你應該已經掌握了如何在uniapp小程序中配置tabbar底部導航欄。無論是基本配置、自定義樣式,還是動態切換,uniapp都提供了靈活的解決方案。希望本文能幫助你在開發過程中更加得心應手。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。