【问题标题】:can nx navigation return multiple actions?nx 导航可以返回多个操作吗?
【发布时间】:2021-06-08 14:17:20
【问题描述】:

我有一个使用 nx 的项目,它使用 nx 的数据持久性来创建 NgRx 效果。

在导航时,我需要为给定路线调度多个操作。但是 nx 导航的预期运行参数格式是:

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

这就是我所拥有的:

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

但这正是我需要的:

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

最后一段代码产生了这个错误:

类型 'TypedAction[]' 不能分配给类型 'void |行动 |可观察的'

这很有意义,因为它期望输出其中一种类型,而我提供了一个 Action 数组。

有没有办法在导航中返回多个操作>运行,就像我对常规效果和开关图所做的一样?

【问题讨论】:

    标签: angular ngrx-effects nrwl-nx nrwl


    【解决方案1】:

    你可以使用:

    of(Action1, Action2)  
    

    或在服务呼叫之后:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多