微信小程序图片加载失败显示默认图片,通常情况下:

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'
     })   
 }

2 Comments

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注