Javascript 使用角度指令延迟加载带有占位符的图像

Javascript 使用角度指令延迟加载带有占位符的图像,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个Angle指令,用于以惰性方式加载图像。代码如下: import { Directive, ElementRef } from '@angular/core'; @Directive({ selector: '[appImgLazy]' }) export class LazyLoadingImgDirective { constructor({ nativeElement }: ElementRef<HTMLImageElement>) { const sup

我有一个Angle指令,用于以惰性方式加载图像。代码如下:

import { Directive, ElementRef } from '@angular/core';

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

    if (supports) {
      nativeElement.setAttribute('loading', 'lazy');
    }
  }
}
但是现在有了那个但现在有了那个
import {
  Directive,
  Attribute,
  Renderer2,
  ElementRef,
  HostListener } from '@angular/core';

@Directive({
  selector: '[appImgLazy]'
})
export class LazyLoadingImgDirective {
  constructor(
    @Attribute('loader') public loader: string,
    @Attribute('onErrorSrc') public onErrorSrc: string,
    private renderer: Renderer2,
    private el: ElementRef,
    { nativeElement }: ElementRef<HTMLImageElement>) {
      const supports = 'loading' in HTMLImageElement.prototype;
      if (supports) {
        nativeElement.setAttribute('loading', 'lazy');
      }
      this.renderer.setAttribute(this.el.nativeElement, 'src', this.loader);
    }

  @HostListener('load') onLoad() {
    this.renderer.setAttribute(this.el.nativeElement, 'src', this.el.nativeElement.src);
  }
  @HostListener('error') onError() {
    this.renderer.setAttribute(this.el.nativeElement, 'src', this.onErrorSrc);
  }
}
  <img
    appImgLazy
    onErrorSrc="src/assets/images/not-found-image.png"
    loader="src/assets/images/placeholder.png"
    [src]="product.cover">
"dependencies": {
  "@angular/animations": "~9.1.6",
  "@angular/cdk": "^9.2.3",
  "@angular/common": "~9.1.6",
  "@angular/compiler": "~9.1.6",
  "@angular/core": "~9.1.6",
  "@angular/forms": "~9.1.6",
  "@angular/localize": "^9.1.12",
  "@angular/platform-browser": "~9.1.6",
  "@angular/platform-browser-dynamic": "~9.1.6",
  "@angular/router": "~9.1.6",
  "@ngx-translate/core": "^12.1.1",
  "@ngx-translate/http-loader": "^4.0.0",
  "bootstrap": "^4.4.1",
  "cookieconsent": "^3.1.1",
  "messageformat": "^2.3.0",
  "ngx-cookieconsent": "^2.2.3",
  "ngx-device-detector": "^1.4.5",
  "ngx-toastr": "^12.0.1",
  "ngx-translate-messageformat-compiler": "^4.7.0",
  "rxjs": "~6.5.4",
  "tslib": "^1.10.0",
  "zone.js": "~0.10.2"
},