微信小程序image加載圖片失敗的處理方法

image方法加載失敗以後,會調用binderror方法,在wxml中先判斷路徑是否爲空,爲空加載默認圖片,不爲空加載路徑圖片,如果出錯,處理出錯方法,換成默認圖片
代碼:

  errorFunction: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      [`hospitals[${index}].CoverUrl`]: "../../../images/appointment/hospital.png",
    })
  },
 <view wx:for="{{hospitals}}" wx:key="Id">
        <view class="hospital-container" data-hospcode="{{item.Name}}" data-image="{{item.CoverUrl}}" bindtap="hospitalItemClick">
          <image class="hospital-image" src="{{item.CoverUrl==''?'../../../images/appointment/hospital.png':item.CoverUrl}}" data-index="{{index}}"mode="scaleToFill"
          binderror="errorFunction"></image>
          ....
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章