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 @   小小强学习网  阅读(139)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2020-09-28 小程序智能识别收货地址
点击右上角即可分享
微信分享提示