I have a store which exposes an asObservable. A view subscribes to this and displays the data. It was working on first load since I've changed it to a Singleton service, when I first go to the page no data is displayed and no subscribe event triggered. After I perform some operation on the data which causes the subject.next to be called it updates and the view updates and everything is great.
I've ensured this is not a race condition problem and that the data is in the store at the time the page's constructor is called.
Sequence of events is as follows (confirmed during debugging):
- Data is loaded from server
- subject.next() is called with correct data
- component constructor is called which subscribes to observer
No subscribe event fires and no data populates, even though it exists in the store. If I then perform some kind of crud operation on the list everything cascades through as expected, next() is called, and a subscribe event occurs.
How can I get the page to read data on page load?
data.store
protected _subject$: Subject<Array<any>>;
this._subject$.next(newData); // confirmed via console this happens with good data
get contacts$(): Observable<any> {
return this._subject$.asObservable();
}
component - I've tried both putting the below block in the constructor and in ngAfterViewInit, same result
// this happens after the above data is already set
this.contacts$.subscribe(data => {
// this block does not execute on page load
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…