ngOnInit或ngAfterViewInit中的内容在加载标签中的所有图像之前,不应加载。

问题描述 投票:1回答:1

我正在将Angular 8用于基于博客的网络应用。数据现在存储在json文件中,甚至图像也将与路径一起加载。

JSON数据

[
    {
        "imgSrc": "./assets/images/dalai-hills-1.jpg",
        "destination": "Dalai Hills",
        "introTitle": "through happy valley, to a picturesque place",
        "place": "mussoorie, uttarakhand",
        "description": "Lorem ipsum dolor sit amet, no elitr tation delicata cum, mei in causae deseruisse.",
    }
]

imgSrc决定加载哪个图像。所有图像均已优化,并放置在资源文件夹中。

模板

<article class="blog-card" style="border-top: 0;" *ngFor="let blog of blogsList">
    <div class="blog-img-wrap" style="min-height: 200px;">
        <a href="#"">
            <img loading="lazy" class="img-fluid blog-img" src="{{ blog.imgSrc }}" alt="blog-image-1">
        </a>
    </div>
</article>

比方说,在博客页面中,由于,加载时加载了12张图像,我想确保仅在加载所有图像之后才加载页面。

我在stackoverflow上没有任何具体答案。当前,加载的文本和图像之间只有几分之一秒的差异,但是看起来很奇怪。

是否有相同的解决方案?

P.S:我想避免使用jQuery。

html angular performance image-loading
1个回答
0
投票

您可以;

  1. 以编程方式创建图像元素。 (使用HTMLImageElement
  2. 跟踪其加载状态。 (使用ReplaySubjectforkJoin
  3. 加载所有图像后,在页面中显示它们。 (使用异步管道Renderer2

这里是一个示例实现(注释中有解释);

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
  data = [
    {
      imgSrc: "https://img.jakpost.net/c/2017/10/27/2017_10_27_34794_1509067747._large.jpg",
      destination: "destination-01",
      introTitle: "introTitle-01",
      place: "place-01",
      description: "description-01"
    },
    {
      imgSrc: "https://lakecomofoodtours.com/wp-content/uploads/gravedona-celiaa-img-0282_orig-800x600.jpg",
      destination: "destination-02",
      introTitle: "introTitle-02",
      place: "place-02",
      description: "description-02"
    },
    {
      imgSrc: "https://italicsmag.com/wp-content/uploads/2020/05/Lake-Como-5-770x550.jpg",
      destination: "destination-03",
      introTitle: "introTitle-03",
      place: "place-03",
      description: "description-03"
    }
  ];

  /* This array holds Observables for images' loading status */
  private tmp: ReplaySubject<Event>[] = [];

  blogData: BlogDataType[] = this.data.map(d => {
    const img = new Image();
    img.height = 200;
    img.width = 300;
    const evt = new ReplaySubject<Event>(1);
    img.onload = e => {
      evt.next(e);
      evt.complete();
    };
    this.tmp.push(evt);
    img.src = d.imgSrc 
    return { ...d, imgElem: img };
  });

  /* 
   * Convert images' loading status observables to a higher-order observable .
   * When all observables complete, forkJoin emits the last emitted value from each.
   * since we emit only one value and complete in img.load callback, forkJoin suits our needs.
   * 
   * when forkJoin emits (i.e. all images are loaded) we emit this.blogData so that we use it
   * in template with async pipe
   */
  blogsList: Observable<BlogDataType[]> = forkJoin(this.tmp).pipe(
    map(() => this.blogData)
  );

  constructor(private renderer: Renderer2) {}

  /* manually append image elements to DOM, used in template */
  appendImg(anchor: HTMLAnchorElement, img: HTMLImageElement) {
    this.renderer.appendChild(anchor, img);
    return "";
  }
}

interface BlogDataType {
  imgElem: HTMLImageElement;
  imgSrc: string;
  destination: string;
  introTitle: string;
  place: string;
  description: string;
}
<div *ngFor="let blog of blogsList | async">
  <div>{{blog.description}}</div>
  <a #aEl href="#">
    {{ appendImg(aEl, blog.imgElem) }}
  </a>
</div>

这里是正在运行的演示:https://stackblitz.com/edit/angular-ivy-ymmfz6

请注意,此实现不是防错的。 img.onerror也应在生产中使用,为简单起见,我跳过了它。

© www.soinside.com 2019 - 2024. All rights reserved.