小程序之图片加载失败问题

1.单个图片加载失败,用默认图片代替

<image src='{{cleanerpicLogo}}' bindload="imageLoad" binderror="imageOnloadError" class='cleanimgs'></image>
// 加载成功
  imageLoad: function (e) {
    console.log("imageLoad"+JSON.stringify(e))
  },
  // 图片加载失败
  imageOnloadError(){
    console.log("加载图片失败");
    this.setData({
      cleanerpicLogo:'/assets/images/cleanimgs.png'
    })
  },

2,列表图片加载失败,用默认图片代替

<image src="{{item.serviceProduct.headImgUrl}}" data-index="{{index}}" mode='widthFix' class='tabsimg' binderror="imageOnloadError"></image>
imageOnloadError(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var serviceInfo = this.data.serviceInfo
    serviceInfo[index].serviceProduct.headImgUrl = '/assets/images/productdetailimg.png';
    this.setData({
      serviceInfo: serviceInfo
    })
  },
 
原文地址:https://www.cnblogs.com/fms-3/p/11199018.html