公共组件errImgae.JS
function errImage(e){
let idx = e.currentTarget.dataset.idx;
let type = e.currentTarget.dataset.type;
let keyNmae = e.currentTarget.dataset.keynmae;
let img = '';
type == 'swiper' ? img = '/img/details_02.png' : img = '/img/logo180.png';
let imgObject = `${type}[${idx}].${keyNmae}`;//修改data数组里面某项需要修改的数据
return {
[imgObject]: img
}
}
module.exports = errImage;
在需要的页面中引入改方法
当图片出错时触发 binderror=“errImage”
<view class="course"
wx:for="{{course}}"
wx:key='index'
data-catId='{{item.catId}}'
data-name='{{item.catName}}'
bindtap="subject"
>
<!--需要把当前数组,和图片字段一起存起来-->
<image class="course-img"
src="{{item.images}}"
mode="aspectFill"
data-type='course'
data-idx='{{index}}'
data-keyNmae='images'
binderror="errImage"
>
</image>
<view class="course-text row-ellipsis">{{item.catName}}</view>
</view>
import errImageUrl from '../../../utils/errImgae.js';
//直接引用,当图片出现损坏是就会触发该方法
errImage(e){
this.setData(errImageUrl(e))
},