Answer the question
In order to leave comments, you need to log in
What are the ways to get data from the NGRX store?
I know that you can get data by creating a subscriber for the rxjs stream. Tried like this:
export class PlayersComponent implements OnInit {
players$: Observable<IPlayer[]>;
players: IPlayer[];
constructor(private store: Store<IAppState>) {
this.players$ = store.pipe(select(selectPlayers));
this.players$.subscribe(getSubscriber(this.players, "Players"));
}
.....
.....
kek() {
this.store.dispatch(new GetPlayers());
console.log('kek: Players -', this.players);
}
}
// создает подписчика для потока для сохранения значений в локальную переменную
// (скорее всего это неправильно), мне хотелось таким образом через *ngFor создать всех игроков
// не знаю как обработать поток players$ правильно в шаблоне
// планировал оттестировать и использовать в других компонентах
function getSubscriber(localStore: IPlayer[], streamName: string) {
return {
next: value => {
console.log('In sub ' ,value);
if (value) localStore = value;
},
error: err => console.error(err),
complete: () => console.debug(`stream ${streamName} - completed`)
}
}
Answer the question
In order to leave comments, you need to log in
My task was to use the data from the storage without creating a bunch of "smart" and "stupid" components (a stream was created in the smart one, and values were passed to the stupid one via '@Input' and pipe 'async'). The problem was solved by using the directive "ngTemplateOutlet" ( click )
But anyway, the question remained open.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question