uni-app sets tabBar's setTabBarBadge shopping cart/message and other corner labels

1. Effect

Please add image description

2. Code implementation

Just use uni.setTabBarBadge and uni.removeTabBarBadge to set and remove the red dot.

Main code:

//设置红点
uni.setTabBarBadge({
    
    
	index: 1, // 底部菜单栏的索引(从0开始)
	text:'99', // 要显示的文本(必须是字符串类型)
});
//移除红点
uni.removeTabBarBadge({
    
    
	index: 1 // 底部菜单栏的索引(从0开始)
});

2. All codes

注意: Add the following code to both 使用页面 and to ensure that you can directly See if there are red dots on the connections. 首页

1.index.vue

Home page

<script>
	export default {
    
    
		data() {
    
    
			return {
    
    
				tabBarNum: '' //底部消息数量
			}
		},
		onLoad() {
    
    
			this.footMsgFun()  //调用底部方法
		},
		onShow() {
    
    
			this.footMsgFun()  //调用底部方法
		},
		methods: {
    
    
			//底部:人脉红点显示
			footMsgFun() {
    
    
				var that = this
				this.$api.appPlateForm('POST', this.$url.all_message, '', function(res) {
    
    
					if (res.code == '200') {
    
    
						//1.获取到接口里,消息的数量
						that.tabBarNum = res.data.num
						
						//2.关键代码:设置红点
						if (that.tabBarNum > 0) {
    
    //设置底部消息通知
							uni.setTabBarBadge({
    
    
								index: 1, // 人脉页面在底部菜单栏的索引
								text: String(that.tabBarNum), // 要显示的文本(必须是字符串类型)
							});
						} else {
    
      //移除底部消息通知
							uni.removeTabBarBadge({
    
    
								index: 1 // 人脉页面在底部菜单栏的索引
							});
						}
					}
				})
			},
			
		}
	}
</script>

2.cart.view

Shopping cart page

<script>
	export default {
    
    
		data() {
    
    
			return {
    
    
				tabBarNum: '' //底部消息数量
			}
		},
		onLoad() {
    
    
			this.footMsgFun()  //调用底部方法
		},
		onShow() {
    
    
			this.footMsgFun()  //调用底部方法
		},
		methods: {
    
    
			//底部:人脉红点显示
			footMsgFun() {
    
    
				var that = this
				this.$api.appPlateForm('POST', this.$url.all_message, '', function(res) {
    
    
					if (res.code == '200') {
    
    
						//1.获取到接口里,消息的数量
						that.tabBarNum = res.data.num
						
						//2.关键代码:设置红点
						if (that.tabBarNum > 0) {
    
    //设置底部消息通知
							uni.setTabBarBadge({
    
    
								index: 1, // 人脉页面在底部菜单栏的索引
								text: String(that.tabBarNum), // 要显示的文本(必须是字符串类型)
							});
						} else {
    
      //移除底部消息通知
							uni.removeTabBarBadge({
    
    
								index: 1 // 人脉页面在底部菜单栏的索引
							});
						}
					}
				})
			},
			
		}
	}
</script>

3. Real cases

		onLoad() {
    
    
			this.getCartData();
		},
		onShow() {
    
    
			this.getCartData();
		},
		mounted() {
    
    
			this.getCartData();
		},
		methods: {
    
    
			getCartData() {
    
    
				let self = this;
				self.isloadding = true;
				self._get('index/index', {
    
    
					url: self.url
				}, function(res) {
    
    
					self.cart_total_num = res.data.cart_total_num;
					if (self.cart_total_num > 0) {
    
    
						uni.setTabBarBadge({
    
    
							index: 3, 
							text: String(self.cart_total_num), 
						});
					} else {
    
     
						uni.removeTabBarBadge({
    
    
							index: 3 
						});
					}
				});
			},
}

reference

Official website
Please use this guy for reference

Gangster 2

at last

If you think the article is good, remember to give it a thumbs up, follow it, and add it to your favorites. Please correct me if there are any mistakes. If you need to reprint it, please indicate the source. Thank you! ! !

Guess you like

Origin blog.csdn.net/m0_49714202/article/details/134827105