【发布时间】:2021-01-26 21:41:20
【问题描述】:
我有两个 Observable,一个依赖于另一个的数据。
当第一个 Observable 上的数据发生变化时,它应该更新第二个 Observable。
很遗憾,这并没有像我预期的那样工作。
这是主要的 Observable(工作正常)
export class LocaleService {
locale$: Observable<string>;
constructor(private router: Router) {
this.locale$ = router.events.pipe(
filter(evnt => envt instanceof ActivationEnd),
map((evnt: ActivationEnd) => evnt.snapshot.paramMap.get('locale')),
shareReplay(1),
);
}
}
这是第二个 Observable,它应该根据第一个 Observable 的值更新(见上):
// This type is more complex, but made it simple for demonstrating the problem
interface ICountries { code: string; }
export class DataService {
countries$: Observable<ICountries>;
constructor(private http: HttpClient, private localService: LocaleService) {
this.localeService.locale$.subscribe(locale => {
// this is only here for debug purposes, showing it does change
console.log('changed locale', locale);
});
this.countries$ = localeService.locale$.pipe(
tap(locale => { console.log('http updated', locale); }), // called first time only, not on locale changes...
switchMap(locale => this.http.get<ICountries>(`http://localhost:8080/countries?lang=${locale}`)),
shareReplay(1),
);
}
}
如果重要的话,我使用countries$ 的方式如下(再次简化以说明问题):
@Component({
template: `<ng-container *ngIf="countries$ | async as countries">{{ countries.code }}</ng-container>`,
})
export class CountryComponent implements OnInit {
countries$: Observable<ICountries>;
constructor(private dataService: DataService) {
this.countries$ = dataService.countries$;
}
}
我已经为此苦苦挣扎了几天,在 StackOverflow、许多其他论坛上寻找答案,并询问了更频繁地使用 Observables 的朋友,但我似乎无法解决这个问题。任何帮助将不胜感激!
【问题讨论】:
-
@MichaelD 正确,只有第一次调用 tap 而不是 locale$ 更改后。我尝试在 LocaleService 上删除
shareReply(1)但这不起作用,因为 DataService 然后没有得到任何更新。还是我误会了你? -
你试过从 local$ observable 中删除 shareReplay(1) 吗?
-
@FatehMohamed - MichaelD 提出了完全相同的问题,但随后删除了他的问题。请参阅我在您问题上方的评论。
-
你订阅你的 observable 吗?也许
dataService.countries$.subscribe();某个我们在这里看不到的地方?真的,shareReplay(1)应该有点像 subscribe(),(它应该导致流评估)但它真的不应该那样使用。不过只是猜测。 -
@MrkSef 在您所看到的之外没有订阅,只是模板中的一个大型数据结构。我尝试在
countries$中添加一个额外的.subscribe(),但不幸的是它没有任何区别。
标签: angular rxjs angular2-observables rxjs-observables