思路:一、調用接口,獲取數值 二、判斷數值是否爲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); } }) }