RXJS 6.0:./node_modules/rxjs-compat/_esm5/add/operator/publishReplay.js 中的错误

RXJS 6.0 : ERROR in ./node_modules/rxjs-compat/_esm5/add/operator/publishReplay.js

将我的应用程序从 angular 5 迁移到 6 后,rxjs 引起了各种问题。我在迁移过程中和删除它之后使用了 rxjs-compact,因为它会导致更大的内存利用率。我一直有这样的错误。

ERROR in ./node_modules/rxjs-compat/_esm5/add/operator/publishReplay.js Module build failed: Error: ENOENT: no such file or directory, open '/home/training/Desktop/vishnu/TemplateAppv6/node_modules/rxjs-compat/_esm5/add/operator/publishReplay.js'

我尝试从 rxjs 和 rxjs/operators 导入 publishReplay。

从 'rxjs/operators';

导入 { filter, map, catchError, publishReplay }

但是问题依然存在,publishReplay有没有像catchError这样的变化。

如有任何帮助,我们将不胜感激。

这是完整的代码

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { ErrorResponse } from '../core/interfaces/Error';

import { throwError, ReplaySubject } from 'rxjs';
import { filter, map, catchError, publishReplay } from 'rxjs/operators';

import 'rxjs/add/observable/forkJoin';

import { environment } from '../../environments/environment';
import { HomeConstants } from './home-consatant';
import { BaseService } from '../core/base.service';




                // Construct the rail data.
                responses.map((response: RailResponse) => {
                  railsData[response.railId] = response
                    .entries
                    .map((entry: EntriesEntity) => {
                      return {
                        imageSrc: this.getImageUrl(entry, response.railId), // Get rail image according to the rail type.
                        contentTypeLabel: entry.pricingType, // Content Type.
                        description: entry.title, // Rail title.
                        url: '/details/' + entry.programType + '/' +
                          this.utility.encodeProgramName(entry.title) + '/' + entry.id, // Rail url.
                        isPopoverVisible: true,
                        popoverTitle: entry.title,
                        popoverDescription: entry.title
                      };
                    });
                });
                return railsData;
              })
              .publishReplay(1, this.cacheInterval)
              .refCount()
              .take(1)
              .catchError((res: HttpErrorResponse) => throwError(res));
            this.rails.set(railParams[0].railId, this.railResponse);
          }
          return this.rails.get(railParams[0].railId);
        }
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

Angular 6 将 RxJS 版本升级到可让运算符的版本。除非安装兼容包,否则您不能再使用以前使用的语法。

新语法是这样的:

import { map } from 'rxjs/operators';
import { Observable, of } from 'rxjs';

let squares: Observable<number> = of(1, 2).pipe(
  map(m => m * m)
);

兼容包:here

还有一种自动方法可以将您的源代码转换为新的(恕我直言,糟糕的)语法。

好的,我终于找到问题并解决了,希望它能对其他人有所帮助,所以我在这里发布。

问题出在管道函数的子函数的排列上。由于新的 rxjs 只支持 pipe 方法,我们过去使用 '.' 给 map 函数的每个子函数。我们必须将子功能与','一起放置。

.map((entry: EntriesEntity) => {
                      return {
                        imageSrc: this.getImageUrl(entry, response.railId), // Get rail image according to the rail type.
                        contentTypeLabel: entry.pricingType, // Content Type.
                        description: entry.title, // Rail title.
                        url: '/details/' + entry.programType + '/' +
                          this.utility.encodeProgramName(entry.title) + '/' + entry.id, // Rail url.
                        isPopoverVisible: true,
                        popoverTitle: entry.title,
                        popoverDescription: entry.title
                      };
                    });
                });
                return railsData;
              })
              .publishReplay(1, this.cacheInterval)
              .refCount()
              .take(1)
              .catchError((res: HttpErrorResponse) => throwError(res));
            this.rails.set(railParams[0].railId, this.railResponse);
          }

将此更改为 ..

 .pipe(map((response: GetRailsResponse) => {
        return response;
      }),
      publishReplay(1, this.cacheInterval),
      refCount(),
      take(1),
      catchError((res: HttpErrorResponse)=> { 
        return throwError(res)
        })
      );


      this.rails.set(JSON.stringify(requestBody), this.railsRes);
    }