ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Angular Component 延迟加载 Lazy Load 的一个依赖注入的问题以及解决方案

2021-07-22 14:03:42  阅读:246  来源: 互联网

标签:Load Lazy container Component product zoom pipe && images


StackOverflow上有个朋友遇到了一个问题:

在 feature module 里,对一个 Component 进行延迟加载:

注意上图第 9 行,导入了 CommonModule.

这个被延迟加载的 Component 的模板文件里,使用到了 async 这个 pipe,其实现在 CommonModule 里。然而,因为该 module 被延迟加载,因此并未静态地定义在 feature module 的 declarations 模块里。所以,该 Component 被加载的时候,其上下文无法访问到 async pipe.

所以,最后会出现运行时错误:

ERROR Error: The pipe 'async' could not be found!

解决方案,我已经回复在 StackOverflow 里了:

https://stackoverflow.com/questions/68258149/the-pipe-async-could-not-be-found-in-spartacus-lazy-loading-of-cms-components

在被延迟加载的 Component 里,将其所属的 feature module 的定义人工加上即可:

import { ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
import { Product, provideDefaultConfig, CmsConfig } from '@spartacus/core';
import { CurrentProductService, MediaModule, OutletModule, CarouselModule } from '@spartacus/storefront';
import { BehaviorSubject, combineLatest, Observable, of } from 'rxjs';
import { distinctUntilChanged, filter, map, tap } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';

@Component({
  selector: 'app-product-images',
  templateUrl: './razer-product-images.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomProductImagesComponent {
  private mainMediaContainer = new BehaviorSubject(null);

  private product$: Observable<Product> = this.currentProductService
    .getProduct()
    .pipe(
      filter(Boolean),
      distinctUntilChanged(),
      tap((p: Product) => {
        this.mainMediaContainer.next(p.images?.PRIMARY ? p.images.PRIMARY : {});
      })
    );

  thumbs$: Observable<any[]> = this.product$.pipe(
    map((p: Product) => this.createThumbs(p))
  );

  mainImage$ = combineLatest([this.product$, this.mainMediaContainer]).pipe(
    map(([, container]) => container)
  );

  constructor(private currentProductService: CurrentProductService) {}

  openImage(item: any): void {
    this.mainMediaContainer.next(item);
  }

  isActive(thumbnail): Observable<boolean> {
    return this.mainMediaContainer.pipe(
      filter(Boolean),
      map((container: any) => {
        return (
          container.zoom &&
          container.zoom.url &&
          thumbnail.zoom &&
          thumbnail.zoom.url &&
          container.zoom.url === thumbnail.zoom.url
        );
      })
    );
  }

  /** find the index of the main media in the list of media */
  getActive(thumbs: any[]): Observable<number> {
    return this.mainMediaContainer.pipe(
      filter(Boolean),
      map((container: any) => {
        const current = thumbs.find(
          (t) =>
            t.media &&
            container.zoom &&
            t.media.container &&
            t.media.container.zoom &&
            t.media.container.zoom.url === container.zoom.url
        );
        return thumbs.indexOf(current);
      })
    );
  }

  /**
   * Return an array of CarouselItems for the product thumbnails.
   * In case there are less then 2 thumbs, we return null.
   */
  private createThumbs(product: Product): Observable<any>[] {
    if (
      !product.images ||
      !product.images.GALLERY ||
      product.images.GALLERY.length < 2
    ) {
      return [];
    }

    return (<any[]>product.images.GALLERY).map((c) => of({ container: c }));
  }
}

@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    MediaModule,
    OutletModule,
    CarouselModule
  ],
  declarations:[CustomProductImagesComponent]
})
export class CustomProductImagesModule {}

更多Jerry的原创文章,尽在:"汪子熙":

标签:Load,Lazy,container,Component,product,zoom,pipe,&&,images
来源: https://www.cnblogs.com/sap-jerry/p/15043720.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有