I am trying to encapsulate the state of ngrx in a common service class in order to abstract implementation details from my components.
An example of a class of service that is registered in my application .module.ts providers
@Injectable() export class PatientService { state: Observable<PatientState>; constructor( private store: Store<AppState>, ) { this.state = store.select<PatientState>('patients'); } }
I checked my actions, the reducer and effects work as expected, however, when I subscribe to the service status in the component, it returns undefined .
An example of a component subscription using a shared service:
@Component({ ... }) export class DashboardComponent implements OnInit { constructor( private patientService: PatientService, ) {} ngOnInit(): void {
If I subscribe directly to the repository, it works as expected.
Example:
@Component({ ... }) export class DashboardComponent implements OnInit { constructor( private patientActions: PatientActions, private store: Store<AppState>, ) {} ngOnInit(): void { this.store.dispatch(this.patientActions.loadPatient()); this.store.select<PatientState>('patients').subscribe(patientState => { console.log('patientState', patientState);
What am I doing wrong?
angular ngrx
Brandon
source share