不要return value with observable,从服务层到组件

Dont return value with observable, from the service layer to the component

我正在尝试通过受组件影响的服务层 return。

我连接的服务api:

 public getPerfilNew$(): Observable<PerfilInvestidor> {
            return this.http.get<PerfilInvestidor>(`${environment.api.basePosicaoConsolidada}/consolidado`)
              .pipe(
                map(res => res),
                shareReplay(1),
                catchError(err => {
                    console.log('Error in perfil', err);
                    return throwError(err);
                })
            )
        }

public getPositionConsolidated(): Observable<PosicaoConsolidada> {
    return this.http.get<PosicaoConsolidada>(`${environment.api.basePosicaoConsolidada}/consolidado`)
      .pipe(
        map(res => res),
        shareReplay(1),
        catchError(err => {
            console.log('Error investiments', err);
            return throwError(err);
        })
    )
}

在我的组件中:我试过了

public loadData() {
        //find out if the profile failed or no
        this.homeGuardService.getPerfilNew$.pipe(
            takeUntil(this.unsubscribe) 
        ).subscribe(res => {
            this.perfilInvestidor = res;
            this.perfilFailed = this.perfilInvestidor.status ? true : false;
            console.log('perfil is failed --->', this.perfilFailed)
        })

        //checking for investments
        this.homeGuardService.getPositionConsolidated().subscribe(res => {
            this.positionConsolidated = res
            if (this.positionConsolidated) {
                this.investments = true
            }
        });


        this.isEverthingFailed = !this.investments && this.perfilFailed
}

我需要外部订阅值来匹配它们在我的变量中 isEverthingFailed teria que utilizar 主题?行为主体?因为这样变量 investmentsperfilFailed 是未定义的

有了那么多的可观察量,我会损害内存吗?我乐于接受建议

1.问题

这里没有足够的代码来确切地看到发生了什么。但从我的角度来看,乍一看它看起来像是一个竞争条件:

你必须了解 Subject 是如何工作的。

const sub = new Subject();
sub.next(1);

// If you subscribe here like:
   sub.subscribe(a => console.log(a))
// Nothing will happen because Subject does not preserve the old values

但是如果你这样做:

const sub = new Subject();

// If you subscribe here like:
sub.subscribe(a => console.log(a))

sub.next(1);

// You will get a print in a console since you emitted the value after you subscribed on the stream.

在您的情况下,看起来您是在主题已经发出值之后订阅主题。

另外:

 this.perfilClient$.subscribe(res => {

您正在订阅 this.perfilClient$,但我没看到您在该流的哪个位置发出值?

因此,如果您无法在 Subject 发出值之前同步您订阅的内容,您可以使用 ReplaySubject 或 BehaviourSubject

2。问题 你只有几个可观察的,它并不多。但是有一些反模式,例如:

  1. 嵌套订阅
  2. 订阅回调函数为空 等..

RXJs 中最主要的是尽可能避免副作用。

您可以使用 forkJoin 或 combineLatest 获取这两个流并订阅它们:

public loadData() {
  forkJoin([this.homeGuardService.getPerfilNew$(), this.homeGuardService.getPositionConsolidated()])
    .subscribe(([perfile, position]) => {
        this.perfilInvestidor = perfile;
        this.perfilFailed = this.perfilInvestidor.status ? true : false;
        console.log('perfil is failed --->', this.perfilFailed)
        this.positionConsolidated = position
        if (this.positionConsolidated) {
            this.investments = true
        }
        this.isEverthingFailed = !this.investments && this.perfilFailed
    })
  )
}