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>
....