判断图片路径是否404
html代码(vue项目)
<view style="display: flex;" v-for="(item,index) in goodsInfo.head_pictures" v-if="goodsInfo.head_pictures[index] == ' '" :key="index">
<image style="width: 100%;" :src="item" mode="widthFix"></image>
</view>
js获取后台数据方法
for(let i=0;i<info.head_pictures.length;i++){
if (this.isImgUrl(info.head_pictures[i]) != 'rejected') {
info.head_pictures[i] = "";
console.log(info.head_pictures)
}
}
this.goodsInfo.head_pictures = info.head_pictures;
console.log(this.goodsInfo.head_pictures)
判断图片是否404方法
// 判断图片是否404
isImgUrl(imgurl) {
return new Promise(function(resolve, reject) {
var ImgObj = new Image(); //判断图片是否存在
ImgObj.src = imgurl;
ImgObj.onload = function(res) {
resolve(res);
}
ImgObj.onerror = function(err) {
reject(err);
}
});
}