/*默认图片*/
Vue.directive('img-url', async function(el, binding) {
const imgURL = binding.value;
if (imgURL && typeof(imgURL) != 'undefined') {
let exist = await imageIsExist(imgURL);
if (exist) {
el.setAttribute('src', imgURL);
} else {
el.setAttribute('src', defaultImg);
}
} else {
el.setAttribute('src', defaultImg);
}
})
/* 检测图片是否存在*/
let imageIsExist = function(url) {
return new Promise((resolve) => {
var img = new Image();
img.onload = function() {
if (this.complete == true) {
resolve(true);
img = null;
}
}
img.onerror = function() {
resolve(false);
img = null;
}
img.src = url;
})
}
vue 默认图片指令
最新推荐文章于 2024-09-02 17:34:07 发布