-1

我有一个 Angular 8 应用程序和一个服务,如下所示:

export class ProfileUserService {
  user$ = this.authService.loginStatus().pipe(take(1));

  constructor(private profileService: ProfileService, private authService: AuthService) {}

  getProfile(): Observable<ProfileApi> {
    return this.user$.pipe(mergeMap(({ profile }) => this.profileService.get(profile.participant)));
  }
}

我有一个组件,我在其中使用调用该方法的服务,如下所示:

export class SettingsAccountComponent extends FormCanDeactivate implements OnInit, OnDestroy {

 constructor(
    private profileUserService: ProfileUserService){}

 ngOnInit() {
    this.innerWidth = window.innerWidth;
    this.profileSubscription = this.profileUserService.getProfile().subscribe((profile: ProfileApi) => {
      this.profile = profile;
      this.deletePicture = false;
      this.buildForm();
    });
  }




}

但是我想直接在组件 SettingsAccountComponent 中调用:这个服务:

private profileService: ProfileService

但问题是这样的:

 user$ = this.authService.loginStatus().pipe(take(1));

因为我需要它来获取参与者 ID。但所以我的问题是,如何组合 ProfileService,像这样

 this.profileSubscription = this.profileService.get().subscribe((profile: ProfileApi) => {
      this.profile = profile;
      this.deletePicture = false;
      this.buildForm();
    });

与:

 user$ = this.authService.loginStatus().pipe(take(1));

因为现在在 get() 方法中它需要一个 ParticipantId

那么我必须改变什么?

谢谢

4

1 回答 1

0

我想aswitchMap可以帮助你。

尝试:

import { switchMap } from 'rxjs/operators';
...
this.profileSubscription = this.profileService.user$.pipe(
  switchMap(({ profile }) => this.profileService.get(profile.participant))
).subscribe((profile: profileAPI) => {
  this.profile = profile;
  this.deletePicture = false;
  this.buildForm();
});

我看你已经mergeMap为你服务了,switchMap很相似。

于 2020-03-31T20:24:14.000 回答