[英]Check if image exists in typescript
我有一個谷歌地圖實現,我使用位於網上的地圖圖塊。 但是如果我走到磁貼區域之外它會拋出我的錯誤,因為它無法找到圖像..如何在if語句不存在時返回null?
我在想的是因為我包含了jquery:
$.get(url)
.done(function() {
// exists code
return `http://www.blablabla.com/grey-tiles/${zoom}/${coord.x}/${coord.y}.png`
}).fail(function() {
// not exists code
return null;
})
但這不適合我,這是我的組件的構造函數:
constructor(){
this.imageMapOptions = {
getTileUrl: (coord: ImageMapTypeCoord, zoom: number) => {
return `http://www.blablabla.com/grey-tiles/${zoom}/${coord.x}/${coord.y}.png`
},
tileSize: { height: 256, width: 256 },
maxZoom: 18,
minZoom: 16,
radius: 1738000,
name: 'Beeksebergen'
};
}
像這樣的東西可能會起作用:
var url = 'https://example.com/image.png';
return new Promise((resolve, reject) => {
return $('<img>')
.on('error', reject.bind(null, url))
.on("load", resolve.bind(null, url))
.appendTo(document.body)
.css({ "position": "absolute", top: -9999, left: -9999 })
.on("error load", $.fn.remove)
.attr("src", url);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.