tabbar添加购物车数字-小程序开发分享

wx.setTabBarBadge({ //购物车不为空 ,给购物车的tabar右上角添加购物车数量标志
index: 2, //标志添加位置
text: "" + res.data.cart_num+ "" //通过编译,将购物车总数量放到这里
})

index为tabbar的索引,text要字符串类型。注意:该api 仅在tabbar页 能调用。如商品详情页(非tabbar页)不能调用

这里 请求后端数据,然后设置数字:

 //获取购物侧数量
    getandSetCartNum:function(){
      var that = this;
      wx.request({
        url: app.globalData.appUrl+'Cart/comcart',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data:{
          appid:app.globalData.appid,
          shop_id:that.data.userInfo.shop_id
        },
        success: function (res){
          
          if(res.data==-9){
            wx.showToast({
              title:'服务接口非法',
              icon:'none'
            })
          }
          console.log("res.data+++++++++++++++++++++++++++++++++++++++++");
          console.log(res.data)
          that.setData({
            cartNum : res.data.cart_num
          })
          if(res.data.cart_num){
            wx.setTabBarBadge({ //购物车不为空 ,给购物车的tabar右上角添加购物车数量标志
              index: 2,                        //标志添加位置
              text: "" + res.data.cart_num+ ""                //通过编译,将购物车总数量放到这里
        })
          }else{   //购物车没有商品  则清除掉
            wx.removeTabBarBadge({
              index: 2
            });
          }
        
        },
        fail:function(res){
          console.log(res.data); 
        }
      })
    },

 

来源:https://blog.csdn.net/qq_36560875/article/details/122077566

posted @ 2022-09-28 15:25  小小强学习网  阅读(120)  评论(0编辑  收藏  举报