Currently, I am working with Angular 11 and NGXS. One issue I am facing involves a subscription for a variable in the state. Here is the problematic subscription:
@Select(state => state.alert.alerts)
alerts$: Observable<any[]>
ngOnInit(): void {
this.alerts$.subscribe(alerts => {
if (alerts) {
this.alertSubject.next(alerts[0])
}
})
}
Unfortunately, this subscription does not work when the object in the state changes.
The structure of my state is as follows:
@Action(AddAlertAction)
addAlert(ctx: StateContext<AlertStateModel>, { text, type }: AddAlertAction) {
const alerts = ctx.getState().alerts
alerts.push({ text, type })
ctx.patchState({ alerts })
}
While this state function operates correctly, the ngOnInit subscription fails to work properly. Any help or insight would be greatly appreciated. Thank you in advance!