我在我的应用中使用了以下服务
@Injectable({providedIn: 'root'})
export class UserSettingsService {
private url = "urlToUserSettings"
private gebruikersettings: UserSettings;
constructor(private client: HttpClient) {
}
GetUserSettings(): Observable<UserSettings> {
return this.client.get<UserSettings>(this.url);
}
}
需要UserSettings的服务需要通过首先订阅GetUserSettings来实现此目的,然后需要执行自己的get方法来创建如下内容
GetSomeData(): Observable<SomeData>{
return this.userSettingsService.GetUserSettings()
.pipe(flatMap(result => {
return this.http.get<SomeData>(`${url}/${result.userCode}`)
.pipe(map(result => result["SomeData"]));
}));
}
随着应用程序变得越来越复杂,它会重复多次,所以我的问题是是否有可能将GetUserSettings()的结果作为属性放在UserSettingsService中,并按如下方式使用该属性
GetSomeData(): Observable<SomeData>{
return this.http.get<SomeData>(`${url}/${userSettingsService.property.userCode}`)
.pipe(map(result => result["SomeData"]));
}
答案 0 :(得分:0)
您为什么不订阅可观察的内容,而只是使用服务方法的结果? 您应该执行以下操作:
enter code hereGetSomeData(): Observable<SomeData>{
this.userSettingsService.GetUserSettings().subscribe(result => {
return this.http.get<SomeData>(`${url}/result`)
.pipe(map(result => result["SomeData"]));
}
}