标签: ngrx-effects

concatLatestFrom 带有多个选择器 ngrx

我正在尝试使用 NgRx 12 中引入的最新 concatLatestFrom 获取 2 个选择器,但是我似乎无法理解我做错了什么,并且我无法实现这一点。

\n

我有一个看起来像这样的效果

\n
  loadAllCases$ = createEffect(() => this._actions$.pipe(\n    concatLatestFrom(() => [\n      this.store.pipe(select(CaseSelectors.getAllCases)),\n      this.store.pipe(select(CaseSelectors.getCasesLoaded))\n    ]),\n    navigation(this.caseLandingPage, {\n      run: (snap, [loaded, cases]) => {\n        if (loaded) {\n          return CaseActions.loadAllSuccess();\n        } else {\n          return this._caseService.getAll().pipe(\n            map(cases => CaseActions.loadAllSuccess(cases))\n          );\n        }\n      },\n      onError: (action, error) => {\n        return CaseActions.loadAllFailed(error);\n      }\n    })\n  ));\n\n
Run Code Online (Sandbox Code Playgroud)\n

然而,由于类型不兼容,这似乎不起作用

\n
Argument of type \'OperatorFunction<Action, [Action, boolean, Case[]]>\' is not assignable to \nparameter of type \'OperatorFunction<Action, ActionOrActionWithState<unknown, Action>>\'. \nType \'[Action, boolean, Case[]]\' …
Run Code Online (Sandbox Code Playgroud)

ngrx ngrx-effects angular ngrx-store nrwl-nx

8
推荐指数
2
解决办法
2万
查看次数

ngrx ofType,@ ngrx/effects

如果我在我的app模块中声明,我试着理解ngrx中的typeof效果是如何工作的:

....

@NgModule({
    imports: [
        EffectsModule.forRoot([TodosEffectsService])
],

....
Run Code Online (Sandbox Code Playgroud)

我写效果文件肯定:

@Effect() createTodos$ = this.actions$
.ofType(CREATE_TASK)
    .map(() => {
        console.log('called');
            return { type: 'OTHER'};
});

@Effect() addTodos$ = this.actions$
.ofType(CREATE_TASK)
    .map(() => {
        console.log('called');
            return { type: 'OTHER'};
});
Run Code Online (Sandbox Code Playgroud)

我试着理解,现在在运行时我调度一个动作this.action $订阅并且每次执行ofType以匹配类型?orType一旦执行!?

如果它调用了一次,当我发出动作时,效果如何知道每次做出订阅/执行的效果?

谢谢大家!

typescript ngrx ngrx-effects angular

7
推荐指数
1
解决办法
6284
查看次数

如何使用ngrx-router-store在ngrx效果中获取路径参数?

我有效果类,我想根据路由器参数ID加载详细信息

@Effect()
  getDetails$ = this.actions$.ofType(DetailActions.GET_DETAILS).pipe(
    map(toPayload),
    switchMap(payload => {
      return this.detailService
        .getDetail(payload)//I want router params here in payload
        .pipe(
          map(detail=> new DetailActions.GetDetailSuccess(detail)),
          catchError(error =>
            Observable.of(new DetailActions.GetDetailFail(error))
          )
        );
    })
  );
Run Code Online (Sandbox Code Playgroud)

我想在有效负载中获取路由器参数,这样我就不必从组件传递有效负载,而是直接从特技类中获取它.

ngrx ngrx-effects angular ngrx-store ngrx-store-4.0

7
推荐指数
1
解决办法
4777
查看次数

有关此Angular + ngRx(效果)的建议 - websocket事件怎么样?

所以我通过构建以下沙箱来试验ngrx&ngrx/effects:

https://stackblitz.com/edit/ngrx-vanilla

快速介绍:

  • 它在app/store中有一个根存储
  • 它在app/features中延迟加载了两个模块
  • 它在app/commons中有单例服务

三页:

  • 行动项目:路由到此页面会触发随机生成三个愚蠢的公司行动项目
  • 用户:具有路由器支持的基本主> detail redux实现
  • 会议:提出问题的地方,点击"开始会议",见证相关的意见交流.

问题和背景:

  • 我理解redux中的所有数据更新都是通过操作实现的
  • "效果"库用于处理异步事件,以便根据第三方事件和异步调用发送新操作.
  • app/common/meeting/service模仿例如websocket或firebase实时数据库推送更新的行为.

收到更新后(在app/store/effects/meeting.effects.ts中说明),将调度新操作.

最后,问题是:让一个共同的服务了解商店是一种干净的做法吗?将一个监听器注册到websocket/firebase实时数据库以便在推送数据时调度操作的最佳位置在哪里?

在这里,我做了一个效果(meeting.effects)对meetingActions.START_MEETING操作类型做出反应,每当推送数据时,都会向商店发送更新订单,但由于我提出的一系列原因,这感觉不对:

  • 难以单独进行单元测试(需要比自身更多的上下文)
  • 在"停止会议"操作的情况下,此方法需要存储订阅(或?)以停止订阅.在我的方法中,无法控制在荒野中创造的可观察物.

这些案件通常如何处理?

redux ngrx ngrx-effects angular ngrx-store

7
推荐指数
1
解决办法
1752
查看次数

NgRx效果中的角度路由器导航

Angular路由器在NgRx效果中是否有任何限制?

我刚开始学习NgRx,我有以下代码:

@Effect() public authenticate$ = this.actions$
    .ofType(authenticationActions.AUTHENTICATE)
        .switchMap((action: AuthenticateAction) => this.authenticationService.authenticate(action.payload)
            .map((data: TokenData) => {
                const user: User = {
                    token: data.token,
                    username: 'dummy',
                };
                console.log(data);
                this.router.navigateByUrl('/');
                return new authenticationActions.AuthenticateSuccessAction(user);
            })
            .catch(error => { console.log(error); return Observable.throw(error); })
        );
Run Code Online (Sandbox Code Playgroud)

控制台记录数据变量并AuthenticateSuccessAction触发操作,因此正在执行路由器线,但导航不会发生.

ngrx ngrx-effects angular angular-router

7
推荐指数
3
解决办法
6514
查看次数

在 ngrx/effect 中访问存储的正确方法

我正在使用 Angular 6、ngrx/store、ngrx/effects。我有一个应该在我按下“保存”按钮时触发的效果。我正在使用withLatestFrom那里收集将其发送到服务器所需的所有数据:

@Effect({dispatch: false})
  saveAll$ = this.actions$.pipe(
    ofType(ActionTypes.Save),
    withLatestFrom(
      this.store.select(fromReducers.getData1),
      this.store.select(fromReducers.getData2),
      this.store.select(fromReducers.getData3),
      this.store.select(fromReducers.getData4)
    ),
    switchMap(([action, data1, data2, data3, data4]: [ActionType, Data1[], Data2[], Data3[], Data4[]]) => {
       // here is some operations with these data
       return this.apiService.saveData({data1, data2, data3, data4})
    })
)
Run Code Online (Sandbox Code Playgroud)

这是getData1选择器:

export const getData1= createSelector(
  getItems,
  getIndexes,
  (items, indexes) => {
    console.log('HI, I AM getData1');
    return transformItems(items, indexes);
  }
);
Run Code Online (Sandbox Code Playgroud)

getItems,反过来,返回state.items。问题是state.items可以修改成另一种效果:

@Effect()
  handleItemsChanges$ = this.actions$.pipe(
    ofType(ActionTypes.ChangesInItems),
    withLatestFrom(
      this.store.select(fromReducers.getItems),
      this.store.select(fromReducers.getUsers), …
Run Code Online (Sandbox Code Playgroud)

javascript ngrx ngrx-effects angular ngrx-store

7
推荐指数
1
解决办法
7012
查看次数

ngrx/effects 方法与 redux-thunk 相比有哪些优势?

我目前正在规划一个大规模的 Angular 6 应用程序,并试图找到一种最适合团队需求的方法来处理副作用。

我意识到在 Ngrx 生态系统中最常用的方法是使用ngrx/effects库,我想知道与thunk方法相比使用它有什么优势,thunk方法似乎是 React 最流行的方法应用。

我的想法是将所有引起副作用的逻辑隔离在一个地方,我总是倾向于将它们隔离在 Action Creators 范围内。将所有副作用逻辑移动到不同的“抽象层”感觉就像会增加编写副作用动作的开销,而没有可观的附加值,因为大多数“强烈逻辑”动作用于处理副作用。

有没有其他理由支持效果而不是 thunk?Angular 中的 ngrx 和 React 的经典 Redux 之间有什么根本区别,这使得 ngrx/effect 成为更好的选择吗?

redux redux-thunk ngrx ngrx-effects angular

7
推荐指数
1
解决办法
1786
查看次数

NGRX 效果调度了无效的操作

我正在尝试@Effect()为我的操作创建一个。当我使用 type 运行 action 时,AuthenticationUserLoad出现错误。

ERROR Error: Effect "AuthenticationEffects.getUser$" dispatched an invalid action: [object Object]
Run Code Online (Sandbox Code Playgroud)

这是我的Effect代码

    @Effect()
      getUser$ = this.actions$.pipe(
       ofType(AuthenticationUserActions.AuthenticationUserTypes.AuthenticationUserLoad),
       map((action) => {

          return this.authService.getUser().pipe(
            map((user: User) => new AuthenticationUserActions.AuthenticationUserLoadSuccess({user})),
            catchError(error => of(new AuthenticationUserActions.AuthenticationUserLoadFailure({error})))

          );
        })
     );
Run Code Online (Sandbox Code Playgroud)

更新

我改变mapswitchMap,它的工作原理。

 @Effect()
  getUser$ = this.actions$.pipe(
    ofType(AuthenticationUserActions.AuthenticationUserTypes.AuthenticationUserLoad),
    switchMap((action) => {

      return this.authService.getUser().pipe(
        map((user: User) => new AuthenticationUserActions.AuthenticationUserLoadSuccess({user})),
        catchError(error => of(new AuthenticationUserActions.AuthenticationUserLoadFailure({error})))
      );
    })
  );
Run Code Online (Sandbox Code Playgroud)

也许我不明白 map 和 switchMap 之间的区别。

typescript ngrx ngrx-effects angular

7
推荐指数
1
解决办法
9704
查看次数

How to test an NGRX effect that uses a store selector?

I have an @Effect that uses a MemoizedSelector to grab an item from the redux store and mergeMap it with the payload of an Action. The effect works just fine, however setting up the Jest tests for this has proven difficult as I cannot seem to mock the return value of the selector due to select being a declared function that's imported (from '@ngrx/store') and used in the effect and the selector itself being an imported function as well. …

rxjs ngrx ngrx-effects angular

7
推荐指数
1
解决办法
1413
查看次数

ngrx 8 影响其他类型的调度动作

我在这里就 ngrx 效果与您联系,我尝试做的是有一个函数登录,它可以分配一个动作类型登录,并且对这个动作的影响将使用我的服务进行 api 调用。在此返回令牌之后,我想调度另外两个操作,一个是 getUserMenu 类型,另一个是 getUserInfo 类型。这两个动作的类型不同,效果也不同。

我最后有 3 个商店:一个用于令牌,一个用于用户信息,一个用于菜单信息

我试过这样的事情:

login = createEffect(
  () =>
  this.actions$
  .pipe(
  ofType(authLogin),
    tap(action => {
    console.log("EFFECT LOGINNNN");
    return this.authService.postLogin(action.username, 
      action.password).pipe(
        map((data: any) => {
          console.log("AUTHTHHTHTH DATATA ", data);
          let props = data.token;
          let payload = {
            token: data.token,
            isAuthenticated: true
          }
      this.store.dispatch(moMenuHttpGetListAction({US_ID: action.username}));  
      this.store.dispatch(userHttpGetInfoAction({US_ID:action.username}));
      this.localStorageService.setItem(AUTH_KEY, payload);
    }))
  })
),
{ dispatch: true }
);
Run Code Online (Sandbox Code Playgroud)

如果我设置 dispatch false 登录工作但没有调用获取用户信息和用户菜单的方法但是当我设置 dispatch true 我有 infinit 循环同样的效果,动作 moMenuHttpGetListAction 看起来像这样:

moMenuHttpGetListEffect = createEffect(
     () => this.actions$.pipe( …
Run Code Online (Sandbox Code Playgroud)

javascript ngrx ngrx-effects angular ngrx-store

7
推荐指数
2
解决办法
9837
查看次数