以下函数从url获取图像并进行加载,然后返回其宽度和高度:
function getImageData (url) {
const img = new Image()
img.addEventListener('load', function () {
return { width: this.naturalWidth, height: this.naturalHeight }
})
img.src = url
}
问题是,如果我做这样的事情:
ready () {
console.log(getImageData(this.url))
}
我得到了,undefined因为该函数运行了,但是图像尚未加载。
仅当照片已加载且宽度和高度已可用时,如何使用等待/异步返回值?
呼啦一阵风
相关分类