欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 image組件binderror使用例子與js中的onerror區(qū)別

 更新時間:2017年02月15日 10:30:12   作者:無影飛絮劍  
這篇文章主要介紹了微信小程序 image組件binderror使用例子與js中的onerror區(qū)別的相關(guān)資料,需要的朋友可以參考下

微信小程序image組件binderror使用例子(對應(yīng)html、js中的onerror)

    官方文檔

 binderror HandleEvent 當(dāng)錯誤發(fā)生時,發(fā)布到 AppService 的事件名,事件對象event.detail = {errMsg: 'something wrong'}

在微信小程序開發(fā)中,我們使用列表包含圖片,如果這個圖片鏈接404錯誤,那么我們應(yīng)該給它提供一個默認(rèn)的友好URL地址。html和js中使用onerror事件就可以了

<img src="image.gif" onerror="this.src='https:w.chesudi.com/Public/web/img/onerrorcar.png'" />

微信小程序image組件沒提供onerror事件,提供了一個binderror事件來代替。

如果圖片鏈接404,就會觸發(fā)這個binderror事件,我們在綁定的事件里修改對應(yīng)的數(shù)據(jù)源就好了,如下

.wxml文件

<image class="carlist_img" src="{{item.img}}" binderror="binderrorimg" data-errorimg="{{index}}"></image>

.js文件 

 binderrorimg:function(e){ 
  var errorImgIndex= e.target.dataset.errorimg //獲取循環(huán)的下標(biāo)
  var imgObject="carlistData["+errorImgIndex+"].img" //carlistData為數(shù)據(jù)源,對象數(shù)組
  var errorImg={}
  errorImg[imgObject]="https://w.chesudi.com/Public/web/img/onerrorcar.png" //我們構(gòu)建一個對象
  this.setData(errorImg) //修改數(shù)據(jù)源對應(yīng)的數(shù)據(jù)
 }

 易錯點:this.setData({"carlistData["+errorImgIndex+"].img":對象})類似這樣的就不正確了

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論