typescript - kombinera flera ngrx-butikval till ett enda anrop Angular 7


Jag behöver i princip göra 3 anrop till min ngrx-butik

Så vad jag hade var

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

nu försöker jag kondensera detta till en enda metod så jag provade detta

Jag importerar rxjs på detta sätt

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

och detta är min metod

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

Jag kallar min metod på detta sätt

ngOninit() {
   this.getStoreData()
}

Mitt problem är att metoden blir anropad men jag får aldrig konsolloggen?

Jag är inte säker på vad jag gör fel

REDIGERA

Jag har också försökt

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

men fortfarande samma problem, ingen console.log()

All hjälp skulle uppskattas!

Svar #1

Jag tror att du behöver prenumerera. Prova antingen detta:

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

eller

ngOninit() {
   this.getStoreData().subscribe()
}

Svar #2

Du kan använda forkJoin för ditt scenario

Exempel :

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

Något liknande detta

export const selectUser = createSelector(
    userState,
    state => state.user
);

Relaterade artiklar