объединить несколько выборок магазина ngrx в один вызов Angular 7 - PullRequest
0 голосов
/ 05 апреля 2019

Мне нужно сделать 3 звонка в мой ngrx магазин

Итак, что у меня было

getUserInfo() {
    this._store.select('userInfo')
       .subscribe(result => this.userInfo = result);
}

getCats() {
    this._store.select('cats')
       .subscribe(result => this.cats = result);
}

getDogs() {
    this._store.select('dogs')
        .subscribe(result => this.dogs = result);
}

, сейчас я пытаюсь сжать это в один метод, чтобы япопробовал это

Я импортирую rxjs примерно так

import { combineLatest } from 'rxjs';
import { tap, filter } from 'rxjs/operators';

и это мой метод

getStoreData() {
    combineLatest(
        this._store.select('userInfo'),
        this._store.select('cats'),
        this._store.select('dogs')
    ).pipe(tap(([userInfo, cats, dogs]) => console.log(userInfo, cats, dogs));
}

Я называю свой метод так

ngOninit() {
   this.getStoreData()
}

моя проблема в том, что метод вызывается, но я никогда не получаю журнал консоли?

Я не уверен, что делаю неправильно

РЕДАКТИРОВАТЬ

Я также пытался

getStoreData {
    forkJoin(
      this._store.pipe(select('userInfo')),
      this._store.pipe(select('xberts')),
      this._store.pipe(select('tasks'))
    ).subscribe(res => console.log(res));
}

, но все та же проблема, нет console.log()

Любая помощь будет оценена!

Ответы [ 2 ]

0 голосов
/ 05 апреля 2019

Я думаю, вам нужно подписаться. попробуйте это:

getStoreData() {
    combineLatest(
        this._store.select('userInfo'),
        this._store.select('cats'),
        this._store.select('dogs')
    ).pipe(filter(e => !e.includes(null)),tap(([userInfo, cats, dogs]) => console.log(userInfo, cats, dogs))
    .subscribe()
}

или

ngOninit() {
   this.getStoreData().subscribe()
}
0 голосов
/ 05 апреля 2019

Вы можете использовать forkJoin для своего сценария

Пример:

   forkJoin(
            this.store.pipe(select('userInfo')),
            this.store.pipe(select('cats')),
            this.store.pipe(select('dogs'))
        ).subscribe(res => { });

Примерно так

export const selectUser = createSelector(
    userState,
    state => state.user
);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...