解决vant--tab栏高亮位置错误的问题

1.点击进入二级路由后,回到一级时,tab栏的高亮会默认跳到第一个(见图2),而不是高亮当前的页面

在项目过程中遇到的,希望能为大家解决问题,谢谢!

正确的案例:

err的案例:

2.vue2用vant页面显示的代码(layout的4个tab按钮):

这里用的icon是云上的,需要引入,复制无效果

<template>
	<div class="box">
		<router-view></router-view>
		<!-- tab -->
		<van-tabbar v-model="active" :safe-area-inset-bottom="false" inactive-color="#333" active-color="#415afc">
			<van-tabbar-item badge="" to="/">
				<span>首页</span>
				<template #icon="props">
					<i :class="['iconfont',`${props.active ? 'icon-shouyefill':'icon-shouye'}`]"></i>
				</template>
			</van-tabbar-item>
			<van-tabbar-item badge="" to="/classify">
				<span>分类</span>
				<template #icon="props">
					<i :class="['iconfont',`${props.active ? 'icon-classify-select':'icon-classfig-un'}`]"></i>
				</template>
			</van-tabbar-item>
			<van-tabbar-item badge="" to="/shopping">
				<span>购物车</span>
				<template #icon="props">
					<i :class="['iconfont',`${props.active ? 'icon-cart-select':'icon-gouwuche'}`]"></i>
				</template>
			</van-tabbar-item>
			<van-tabbar-item badge="" to="/my">
				<span>我的</span>
				<template #icon="props">
					<i :class="['iconfont',`${props.active ? 'icon-mine-select':'icon-mine-unselect'}`]"></i>

				</template>
			</van-tabbar-item>
		</van-tabbar>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				active: 0, // 让底部高亮
			}
		},
	}
</script>

<style lang="scss" scoped>
	.box {
		height:100%;
		background-color: #fafafa;
	}
	::v-deep .van-tabbar {
		height:60px;
		padding-bottom:20px;
		z-index: 999;
	}
	::v-deep .van-tabbar-item__icon img{
		height:24px;
		width:27px;
		object-fit:contain;
		//控制图片如何在 img 元素里显示
	}
	::v-deep .van-tabbar-item__text{
		font-weight:bold;
	}
</style>

3.解决方案:

1.运用侦听器监听路由变化

watch: {
			$route: function(newV) {
				if (newV.path == '/' || newV.path == '/home') {
					this.active = 0;
				} else if (newV.path == '/classify') {
					this.active = 1;
				} else if (newV.path == '/shopping') {
					this.active = 2;
				} else if (newV.path == '/my') {
					this.active = 3;
				}
			}
		},

2.钩子函数强制控制


		mounted() {
			if (this.$route.path == '/' || this.$route.path == '/home') {
				this.active = 0;
			} else if (this.$route.path == '/classify') {
				this.active = 1;
			} else if (this.$route.path == '/shopping') {
				this.active = 2;
			} else if (this.$route.path == '/my') {
				this.active = 3;
			}
		}

4.解决完毕

如有更好的方案或解决方法可以说出来哦,谢谢,若有错误,请指正!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

じòぴé南冸じょうげん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值