【问题标题】:need help on angular 7 Observable async pipe在 Angular 7 Observable 异步管道上需要帮助
【发布时间】:2020-05-12 00:56:45
【问题描述】:

我正在尝试通过 API 调用实现页面源等待

    public responseTop: Observable<any>;
    this.responseTop = this.productSRV.topEquipments();
    this.responseTop.toPromise().then(sucess => {
        this.topEquipments = sucess['data'];
        // console.log('eq');
        // console.log(this.topEquipments);
        this.newsletterForm.controls.recaptchaReactive.setValue(sucess['message']);
      }).catch(error => {
        console.log('Error!', error);
    });

  <ng-container *ngFor="let topEq of topEquipments;">

因为API调用了两次,怎么放|异步等待 SEO 的页面源 如果我把

  <ng-container *ngIf="responseTop | async">

仍然不会在页面源中加载

【问题讨论】:

    标签: angular rxjs angular7 url-routing angular-universal


    【解决方案1】:

    一旦你调用.then你第一次执行它,当你使用async它第二次执行它。

    问题是 - 期望的行为是什么?

    我建议将它们组合在一起:

    public responseTop: Observable<any>;
    this.responseTop = this.productSRV.topEquipments().pipe(
      // side effect
      tap(success => this.newsletterForm.controls.recaptchaReactive.setValue(success['message'])),   
    
      // mapping result to the array we need
      tap(console.log),
      map(success => success['data']),
    
      // catching error
      catchError(error => {
        console.log('Error!', error);
        throw error;
      }),
    );
    
    <ng-container *ngIf="responseTop | async as topEquipments">
      <ng-container *ngFor="let topEq of topEquipments">
        {{ topEq | json }}
      </ng-container> 
    </ng-container>
    
    

    【讨论】:

    • 行为是实现topEquiment生成的html在做build:ssr和serve:ssr时应该出现在页面源码中。 catchError 给我这个错误 'MonoTypeOperatorFunction' 类型的参数不可分配给'() => void'.ts(2345) 类型的参数
    • 如果您评论tap,它会抛出错误吗?你也可以分享productSRV.topEquipments()的来源吗?
    • "rxjs": "~6.3.3", "rxjs-compat": "^6.3.3", 我正在使用这个版本的 rxjs topEquipments() { return this.http.get (${this.apiUrl}topCategories, this.helperSRV.getHeaders(true)).pipe(map(this.extractData)); }
    • 没关系,我认为问题出在定义发生的地方,或者它导致的副作用。
    • aha,所以它可以工作,但 success['data'] 不是数组。可以分享一下success 是什么吗?
    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    相关资源
    最近更新 更多