uni-app打开非tabBar页面时定义新的tabBar(底部导航)(自定义tarBar组件)

使用uni-app开发的时候遇到这一个需求,一级页面有自己的tabBar当跳转到一个新的页面时,出现另一个自己的底部导航(tabBar)

默认打开页面时
在这里插入图片描述
在跳转一个新的页面时的底部导航
在这里插入图片描述

默认打开的的底部导航,可以通过pages.json来配置tabBar
详情参考uni官网https://uniapp.dcloud.io/collocation/pages?id=tabbar3

但是要实现跳转到一个全新的页面,在去定义一个新的tabBar时,光靠配置pages.json是做不到的,这时候需要我们自己去定义封装局部tabBar组件来实现

封装的tabBar.vue页面

<template>
	<view class="tarbar">
		<view v-for='(item,index) in datas' @tap="tabClick(index,item.urls,item.id)">
			<u-icon :color="item.color" :name="item.icon" size="60"></u-icon>
			<view v-text="item.name"></view>
		</view>
	</view>
</template>

<script>
	export default {
		props:["totarBer"],//从父组件传来的值 来控制点击当前的颜色
		data() {
			return {
				datas: [{
						urls: '/pages/home/home',
						icon: 'home',
						name: '首页',
						id: 1,
					},
					{
						urls: '/pages/tcpj/shakeDown',
						icon: 'order',
						name: '采购条件',
						id: 2,
					},
					{
						urls: '/pages/tcpj/orderSucceed',
						icon: 'checkbox-mark',
						name: '接单成功',
						id: 3,
					},
				],
			}
		},
		created() {
		//用来控制是显示当前页面的tarBar图标变亮
			if(this.totarBer.id==2){
				this.$set(this.datas[2],'color',this.totarBer.color)
			}
			if(this.totarBer.id==1){
				this.$set(this.datas[1],'color',this.totarBer.color)
			}
			
			
		},
		methods: {
			tabClick(i,urls, id) {
				if(id==1){
					uni.reLaunch({
						url: urls
					});
				}else{
					uni.navigateTo({
						url: urls
					});
				}
				
            
			}
		}
	}
</script>

<style scoped>


	.tarbar {
		width: 100%;
		height: 96rpx;
		display: flex;
		justify-content: space-around;
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		z-index: 100;
		font-size: 30rpx;
		color: #999999;
	}

	.tarbar view {
		text-align: center;
	}
</style>

采购条件的页面

<template>
	<view class="warp">
		<!-- 底部导航 -->
		<view>
			<tarBer :totarBer='tar'></tarBer> //使用tarBar组件
		</view>
	</view>
</template>
<script>
	import tarBer from './tarber.vue' //先把tabBar.vue组件导入
 export default{
	 data(){
		 return {
			 tar:{
				 color:'#07A7E3',
				 id:1
			 },//向子组件发送的值
		 }
	 },
	 components:{
		 tarBar
	 }
 }
</script>
<style scoped>
	.warp {
		color: #000000;
	}
</style>

同样哪个页面需要使用新的tabBar导航 ,先导入 在使用

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
uni-app中,你可以通过以下步骤来实现自定义底部tabbar: 1. 在main.js文件中添加以下代码段,以解决点击两次才能选择icon的问题: ``` Vue.mixin({ methods: { setTabBarIndex(index) { if (typeof this.$mp.page.getTabBar === 'function' && this.$mp.page.getTabBar()) { this.$mp.page.getTabBar().setData({ selected: index }) } } } }) ``` 2. 在index.wxss文件中重新定义tabbar的样式,包括背景、高度、字体大小等。以下是一个例子: ```css .tab-bar { position: fixed; bottom: 0; left: 0; right: 0; display: flex; box-shadow: 0px -2px 10px 0px rgba(0,0,0,0.05); box-sizing: content-box; } .tab-bar-item { flex: auto; text-align: center; display: flex; justify-content: center; align-items: center; flex-direction: column; background: #fff; height: 120rpx; } /* 自定义样式 */ .tab-bar-item.diy { margin-top: 0!important; background: transparent; position: relative; flex: inherit; width: 134rpx; } .tab-bar-item image { width: 48rpx; height: 48rpx; overflow: initial; } .tab-bar-item view { font-size: 24rpx; } .tab-bar-item image.diy { position: absolute; width: 134rpx; height: 140rpx; bottom: 25.6%; z-index: 100; } .tab-bar-item view.diy { margin-top: 90rpx; background: #fff; width: 100%; height: 100%; padding-top: 58rpx; z-index: 99; } ``` 通过以上步骤,你可以在uni-app中实现自定义底部tabbar。使用以上的代码段和样式定义,你可以根据需求自定义底部tabbar的样式和交互效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uni-app-tabbar:uni-app底部初步实现(不支持小程序)](https://download.csdn.net/download/weixin_42172572/15923240)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uni-app自定义底部tabbar](https://blog.csdn.net/Janent168/article/details/129809136)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值