思路:一、调用接口,获取数值 二、判断数值是否为0,若是数值为0,调用wx.removeTabBarBadge()函数,若是数值不为0,调用wx.setTabBarBadge()函数;以下ajax
/** * 获取用户购物车数量 */ 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); } }) }