<dt><img v-bind:src="getHeadPic(user.headPic)" alt=""></dt>
methods:{
getHeadPic:function(headPic){
if(headPic !=null&& headPic != undefined && headPic != ''){
return headPic ;
}
return"http://woxin2.jx139.com/cztx/images/ic_default_head.png";
}
通过vue.js动态绑定的方法,返回默认的头像,这种情况只考虑到headPic 为 null 、undefined、 ”''的这三种情况,而没有考虑到返回的地址为404的情况,通过上网查资料得知,img自身就带有 onerror属性,可以监控到图片是否加载失败的情况,于是乎,将代码调整如下
<img v-bind:src="getHeadPic(user.headPic)" alt="" onerror="this.onerror=null; this.src='http://woxin2.jx139.com/cztx/images/ic_default_head.png'">
安卓没有问题 可以识别 onerror ios出现循环加载情况,无法识别onerror事件