uniapp自定义底部tabBar

使用场景:在一个非tabbar页面,想要有底部导航效果,故自定义效果,系统原底部导航栏仍在正常使用

效果:

布局:

html 复制代码
<template>
	<view class="tab-bar" :style="{height: height + 'px'}">
		<view v-for="(item, index) in tabBars" :key="index" @click="switchTab(item)"
			:style="{color: index === selectIndex ? item.selectedTextColor : item.textColor}">
			<view style="display: flex; justify-content: center;">
				<image :src="index === selectIndex ? item.selectedIconPath : item.iconPath" style="height: 25px; width: 25px;"></image>
			</view>
			<view style="margin-top: 2px; font-size: 12px;">
				{{item.text}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"tabBar",
		props: {
			tabBars: {
				type: Array,
				required: true
			},
			selectIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				height: undefined
			};
		},
		methods: {
			switchTab(item) {
				if (this.getCurrentPageIndex() !== item.pagePath) {
					uni.redirectTo({
						url: item.pagePath
					});
				}
			},
			getCurrentPageIndex() {
				const pages = getCurrentPages();
				return pages[pages.length - 1].route;
			}
		},
		mounted() {
			let systemInfo = uni.getSystemInfoSync();
			let tabBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight;
			this.height = tabBarHeight
		}
	}
</script>

<style>
.tab-bar {
  display: flex;
  justify-content: space-around;
  position: fixed;
  height: 5vh;
  bottom: 0;
  width: 100%;
  background-color: #fbfcfe;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  padding-top: 3px;
  padding-bottom: 2px;
}
</style>

使用:

html 复制代码
<tabBar :tab-bars="approvalTabbar" :selectIndex="1"></tabBar>
// selectIndex 是当前页面的数组下标
approvalTabbar = [
	{
		iconPath: '/static/icon/mess.png',
		selectedIconPath:'/static/icon/mess-selected.png',
		text: "发起申请",
		pagePath:'/pages/approval/launch/launch',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	},
	{
		iconPath: '/static/icon/approval-mine.png',
		selectedIconPath:'/static/icon/approval-mine-selected.png',
		text: "我审批的",
		pagePath:'/pages/approval/accraditation/accraditation',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	},
	{
		iconPath: '/static/icon/submit.png',
		selectedIconPath:'/static/icon/submit-selected.png',
		text: "已提交",
		pagePath:'/pages/approval/submit/submit',
		textColor: tabTextColor,
		selectedTextColor: tabSelectedTextColor
	}
]

不足之处:

1.需要在参与跳转的每个页面引入组件

2.引入时需要传递相同的tabs数组数据、页面与数组对应的下标

3.页面跳转后会重绘底部导航栏,出现闪的问题

相关推荐
2501_916007473 小时前
iOS文件管理工具深度剖析,从系统沙盒到跨平台文件操作的多工具协同实践
android·macos·ios·小程序·uni-app·cocoa·iphone
shykevin3 小时前
uni-app x开发商城系统,扩展组件uni-ui实现底部商品导航
uni-app
QuantumLeap丶3 小时前
《uni-app跨平台开发完全指南》- 05 - 基础组件使用
vue.js·微信小程序·uni-app
2501_915918415 小时前
Flutter 加固方案对比与实战,多工具组合的跨平台安全体系(Flutter App 加固/IPA 成品混淆/Ipa Guard CLI/自动化安全流程)
安全·flutter·ios·小程序·uni-app·自动化·iphone
00后程序员张20 小时前
iOS 上架费用全解析 开发者账号、App 审核、工具使用与开心上架(Appuploader)免 Mac 成本优化指南
android·macos·ios·小程序·uni-app·cocoa·iphone
前端与小赵1 天前
uni-app开发安卓app时控制屏幕常亮不息屏
android·gitee·uni-app
2501_916008891 天前
HTTPS 请求抓包,从原理到落地排查的工程化指南(Charles / tcpdump / Wireshark / Sniffmaster)
ios·小程序·https·uni-app·wireshark·iphone·tcpdump
xiaohe06011 天前
🥳 Uni ECharts 2.1 发布:正式支持鸿蒙,零成本迁移、全平台兼容、跨端开发零负担!
vue.js·uni-app·echarts
2501_915909062 天前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview