微信小程序图片加载失败显示默认图片,通常情况下:
WXML:
<image class="userinfo-avatar" src="{{avatar}}" binderror="errorFunction"></image>
JS:
errorFunction: function(){ this.setData({ avatar: '/image/head.png' }) }
若使用了 for 循环,代码如下
WXML:
<view wx:for="{{branchList}}" wx:key="{{key}}" wx:for-index='index'> <image src='{{item.branch_photo}}' binderror="errorFunction" data-index='{{index}}'></image> </view>
JS:
errorFunction: function (event) { console.log(event) var index = event.currentTarget.dataset.index var img = 'branchList['+index+'].branch_photo' this.setData({ [img]: '/pages/resource/img/loading.gif' }) }
wad
牛逼牛逼