Search code examples
angulartypescriptrxjsobservablengxs

How to subscribe to meta selectors (combine state selectors) in ngxs?


I looking at the docs on the NGXS official page: https://ngxs.gitbook.io/ngxs/concepts/select#meta-selectors

I want to retrieve the state of multiple states therefore have to use their proposed way of doing it:

      export class CityService {
      @Selector([Zoo, ThemePark]) 
      static zooThemeParks(zoos, themeParks) {
        return [
          ...zoos,
          ...themeParks
        ];
      }
    ​
    }

How do you correctly consume this Selector? How to trigger it inside a component, possibly via Observables and subscribe?

I'm using the NGXS latest version.


Solution

  • The problem was that the docs did not state that the zoos and themeParks are arrays as well, mine were objects {} therefore got undefined.

    This is an ES6 problem but I think there should be an extra comment for such things in the docs.

    instead of

    return [
              ...zoos,
              ...themeParks
           ];
    

    I had to put

    return {
               ...zoos,
               ...themeParks
           };