思路:1、调用接口,获取数值 2、判断数值是否为0,如果数值为0,调用wx.removeTabBarBadge()函数,如果数值不为0,调用wx.setTabBarBadge()函数;如下 /** * 获取用户购物车数量 */ set_nav_cart_num:function () { var that = this; that.post_ajax(’/index/shop_cart_namber’,{ user_id: that.user_id, }, function (res, status) { // console.log(res) if (status == 200) { if(res != 0){ wx.setTabBarBadge({ index: 2, text: res + ‘’, }) }else{ wx.removeTabBarBadge({ index: 2, text: ‘’, }) } console.log(res); } else { app.basic_dialog(res); } }) }