[Angular] Lazy Load Images in Angular

<img src="src.png" alt="Angular" loading="lazy"> 
import { Directive, ElementRef } from '@angular/core';

@Directive({ selector: 'img' })
export class LazyImgDirective {
  constructor({ nativeElement }: ElementRef<HTMLImageElement>) {
    const supports = 'loading' in HTMLImageElement.prototype;

    if (supports) {
      nativeElement.setAttribute('loading', 'lazy');
    } else {
      // fallback to IntersectionObserver
    }
  }
}

Read More

原文地址:https://www.cnblogs.com/Answer1215/p/13599189.html