小程序的image組件不像普通html 的image,沒有onerror屬性,不過有個binderror回調方法。卻不像onerror="this.src='/static/img/fmdefault.png'"來的方便。
binderror 當錯誤發生時,發布到 AppService 的事件名,事件對象event.detail = {errMsg: 'something wrong'};
當圖片錯誤時,調用binderror方法,然后去改變image的src綁定的值。
<image class="userinfo-avatar" src="{{avatar}}" binderror="errorFunction"></image> errorFunction: function(){ this.setData({ avatar: '/image/head.png' }) }