Heim > Fragen und Antworten > Hauptteil
function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
var image = new Image();
image.onload = function() {
resolve(image);
};
image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
image.src = url;
});
}
Was ich wissen möchte, ist, wie man diese Methode anwendet? Ich habe URL
,然后再then
eingegeben, um das Objektbild zu erhalten? Aber ich habe es getestet, aber es kam keine Antwort!
滿天的星座2017-05-16 13:31:18
loadImageAsync('./img/news-1.png').then((img) => {
document.getElementById("app").appendChild(img)
console.log(img)
})
这么调用,在then的参数就是resolve的参数, 也就是image对象,把这个对象append到p上就可以实现功能
高洛峰2017-05-16 13:31:18
loadImageAsync(url).then(function(img) { doSomething(); }).catch(function(err) { handleError(err); });
我想大声告诉你2017-05-16 13:31:18
可以的 ,我在浏览器里试过了:
function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
var image = new Image();
image.onload = function() {
resolve(image);
};
image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
image.src = url;
});
}
loadImageAsync('https://www.baidu.com/img/bd_logo1.png').then(function(){alert("jiazai wancheng")})