如何使用角度4检查资产文件夹中是否存在图像

Kar*_*rty 5 angular-cli angular

我需要使用Angular 4检查文件夹中是否存在图像.我有一个从服务器返回的图像名称列表.我需要的是检查资产文件夹中是否存在该图像.任何帮助或想法都会很棒.

Pat*_*dak 6

试试这个.在链接上找到答案

// The "callback" argument is called with either true or false
// depending on whether the image at "url" exists or not.
function imageExists(url, callback) {
  var img = new Image();
  img.onload = function() { callback(true); };
  img.onerror = function() { callback(false); };
  img.src = url;
}

// Sample usage
var imageUrl = 'http://www.google.com/images/srpr/nav_logo14.png';
imageExists(imageUrl, function(exists) {
  console.log('RESULT: url=' + imageUrl + ', exists=' + exists);
});
Run Code Online (Sandbox Code Playgroud)

我想警告你.由于它将图像加载到内存(如果存在),它可以帮助您解决性能问题,以便立即检查更多图像.