如何从两个服务变成一个服务?
How to make from two services one service?
我有一个 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));
因为我需要它来获取 participantId。但是我的问题是,如何组合 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
那么我必须改变什么?
谢谢
我觉得 switchMap
可以帮到你。
尝试:
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
非常相似。
我有一个 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));
因为我需要它来获取 participantId。但是我的问题是,如何组合 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
那么我必须改变什么?
谢谢
我觉得 switchMap
可以帮到你。
尝试:
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
非常相似。