can nx navigation return multiple actions?

Viewed 234

I have a project that uses nx, and it is using nx' data persistence for creating NgRx effects.

On navigation, I need to dispatch multiple actions for a given route. however the expected run parameter format for nx navigation is:

(parameter) run: (a: ActivatedRouteSnapshot, state?: T) => Observable<Action> | Action | void

this is what I have:

createEffect(() =>
  this.actions.pipe(
    withLatestFrom(this.store.select(myStateSelector)),
    navigation(component, {
      run: (a: ActivatedRouteSnapshot, state: MyState) => {
              if (state.isLookup1Loaded) return;
              return myActions.loadLookup1();
           },
      onError: (action, error) => {
        throw Error('error');
      }
    })
  )
);

but this is what I need:

createEffect(() =>
  this.actions.pipe(
    withLatestFrom(this.store.select(myStateSelector)),
    navigation(component, {
      run: (a: ActivatedRouteSnapshot, state: MyState) => [
            myActions.loadLookup1(),
            myActions.loadLookup2()
           ],
      onError: (action, error) => {
        throw Error('error');
      }
    })
  )
);

that last piece of code, generates this error:

Type 'TypedAction[]' is not assignable to type 'void | Action | Observable'

which makes perfect sense given that it is expecting an output of one of those types, and I am providing an array of Action instead.

is there a way to return multiple actions in that navigation>run, same as I would do with a regular effect and a switchmap?

1 Answers

You could use:

of(Action1, Action2)  

or after a service call:

.pipe(mergeMap(()=>[Action1, Action2]))
Related