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>