基于Vue3+UniApp在單個頁面實現固定TabBar的多種方式
在 UniApp 開發(fā)中,tabBar 通常是通過 pages.json 配置的,適用于整個應用的全局導航

然而,在某些場景下,我們可能需要只在特定的頁面展示 tabBar,而不會影響其他頁面的布局。這就需要使用 自定義 tabBar,可以通過 view 組件、uni-segmented-control 組件或 uni-nav-bar 組件等方式來實現
以下是幾種適用場景:
局部頁面導航:
例如,一個訂單管理頁面,用戶可以在 “進行中” 和 “已完成” 之間切換,而不影響全局 tabBar底部固定的二級導航:
例如,在 “記錄” 頁面,提供 “待審核” 和 “已審核” 選項,方便用戶切換,而不會影響其他頁面頂部導航欄的切換:
適用于不適合使用底部 tabBar 的場景,如數據管理頁面,用戶可以在 “待審核” 和 “已審核” 之間切換
| 方法 | 適用場景 | 說明 |
|---|---|---|
| 方法 1:自定義 view tabBar | 僅在某個頁面底部 | 適用于特定頁面,不會影響全局 tabBar |
| 方法 2:uni-segmented-control | 輕量級頁面切換 | 適用于簡單的 Tab 選項,不會影響布局 |
| 方法 3:uni-nav-bar | 頂部導航切換 | 適用于帶頂部導航的 UI |
1. 自定義 view
<template>
<view class="container">
<!-- 固定底部的 tabBar -->
<view class="fixed-tabbar">
<view class="tab-item" :class="{ active: currentTab === 0 }" @click="currentTab = 0">
<text>測試A</text>
</view>
<view class="tab-item" :class="{ active: currentTab === 1 }" @click="currentTab = 1">
<text>測試B</text>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
currentTab: 0, // 當前選中的 tab
};
}
};
</script>
<style scoped>
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.content {
flex: 1;
overflow-y: auto; /* 讓內容可以滾動 */
padding-bottom: 60px; /* 避免被底部 tabBar 遮擋 */
}
.fixed-tabbar {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 50px;
display: flex;
justify-content: space-around;
align-items: center;
background-color: #fff;
border-top: 1px solid #ddd;
}
.tab-item {
flex: 1;
text-align: center;
padding: 10px;
font-size: 14px;
color: #666;
}
.tab-item.active {
color: #007AFF;
font-weight: bold;
}
</style>
截圖如下:
fixed-tabbar 采用 position: fixed; bottom: 0;,始終固定在頁面底部
使用 currentTab 變量控制當前選中的 tab,并根據 active 類名高亮
padding-bottom: 60px; 避免頁面內容被 tabBar 遮擋

2. uni-segmented-control
以輕量級方式切換不同的頁面內容,不需要固定的底部 tabBar
<template>
<view>
<!-- 頂部 Tab 切換 -->
<uni-segmented-control
:current="currentTab"
:values="['測試A', '測試B']"
@clickItem="switchTab"
/>
<!-- 提柜記錄 -->
<view v-if="currentTab === 0">
<uni-card title="測試A">
<text>這里是測試A...</text>
</uni-card>
</view>
<!-- 殘損單記錄 -->
<view v-else>
<uni-card title="測試B">
<text>這里是測試B...</text>
</uni-card>
</view>
</view>
</template>
<script>
export default {
data() {
return {
currentTab: 0 // 默認選中第一個
};
},
methods: {
switchTab(index) {
this.currentTab = index;
}
}
};
</script>
截圖如下:
uni-segmented-control 提供頂部 tab,點擊時 @clickItem 觸發(fā) switchTab 進行切換
僅 currentTab === 0 時展示
適用于無需固定底部導航欄的場景,如數據篩選切換

3. uni-nav-bar
頂部導航,在標題欄左右提供不同的 Tab 切換按鈕
<template>
<view>
<!-- 自定義頂部 Tab -->
<uni-nav-bar >
<template v-slot:left>
<view @click="switchTab(0)" :class="{ active: currentTab === 0 }">測試A</view>
</template>
<template v-slot:right>
<view @click="switchTab(1)" :class="{ active: currentTab === 1 }">測試B</view>
</template>
</uni-nav-bar>
<!-- 頁面內容 -->
<view v-if="currentTab === 0">
<uni-card title="測試A">
<text>這里是測試A...</text>
</uni-card>
</view>
<view v-else>
<uni-card title="測試B">
<text>這里是測試B...</text>
</uni-card>
</view>
</view>
</template>
<script>
export default {
data() {
return {
currentTab: 0
};
},
methods: {
switchTab(index) {
this.currentTab = index;
}
}
};
</script>
<style scoped>
.active {
font-weight: bold;
color: #007AFF;
}
</style>
uni-nav-bar 作為導航欄,在左右 slot 里添加點擊切換的按鈕。
switchTab 控制當前顯示的內容

以上就是基于Vue3+UniApp在單個頁面實現固定TabBar的多種方式的詳細內容,更多關于Vue3 UniApp固定TabBar的資料請關注腳本之家其它相關文章!
相關文章
vue測試環(huán)境打包與生產環(huán)境打包文件數量不一致解決方案
這篇文章主要為大家介紹了vue測試環(huán)境打包與生產環(huán)境打包文件數量不一致的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
Vue+Element-ui表單resetFields無法重置問題
本文主要介紹了Vue+Element-ui表單resetFields無法重置問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
vue2+springsecurity權限系統(tǒng)的實現
本文主要介紹了vue2+springsecurity權限系統(tǒng)的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
Vue 3.0 前瞻Vue Function API新特性體驗
這篇文章主要介紹了Vue 3.0 前瞻Vue Function API新特性體驗,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

