E
E
even__odd2020-05-07 19:46:34
Angular
even__odd, 2020-05-07 19:46:34

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`)
  }
}


Are there any good articles on component life cycle? About how it is created, how the constructor is called and what parameters are passed there.
More recently, I became interested in Angular and NGRX.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
even__odd, 2020-05-11
@even__odd

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 question

Ask a Question

731 491 924 answers to any question