【问题标题】:ngrx effects - Use a selector inside an effect after using a servicengrx 效果 - 使用服务后在效果内使用选择器
【发布时间】:2020-11-16 23:32:39
【问题描述】:

我需要进行 API 调用。 我需要从我的效果内的商店中获取一些数据。 已经尝试使用 withLatestForm 但据我了解,您只能在开始时使用它。帮助将不胜感激。

    sendTestStop = createEffect(() => {
        return this.actions$.pipe(
            ofType('[API] do it'),
            switchMap(() => this.RestApiService.DoCommand()),
            switchMap(
                    ((value: TestStopResponse) => {
                        let response = [];
                        for (const test of value.ResponseArray) {
                            // here i need some data from the store <-------------------------
                            if(test.IsStopped){
                                response.push(this.viewActions.action({logString: "logmsg"}));
                                response.push(this.activeTestActions.action2({test: test}));
                            }
                            else{
                                response.push(this.viewActions.addLogEntry({logString: "logmsg"}));
                            }

                        }
                        return response;

                    })),
            )
    });

【问题讨论】:

    标签: typescript rxjs ngrx


    【解决方案1】:

    查看@Priyamal 答案下的 cmets,了解为什么这个解决方案对我的情况是正确的。

    sendTestStop = createEffect(() => {
        return this.actions$.pipe(
            ofType('[API] do it'),
            mergeMap(() => this.RestApiService.DoCommand()),
            withLatestFrom(this.store.pipe(select(this.testSelector.selectAllTestSetsArray))),
            switchMap(
                    ([apiValue, storeValue]) => {
                        let response = [];
                        for (const test of value.ResponseArray) {
                            // here i need some data from the store <-------------------------
                            if(test.IsStopped){
                                response.push(this.viewActions.action({logString: "logmsg"}));
                                response.push(this.activeTestActions.action2({test: test}));
                            }
                            else{
                                response.push(this.viewActions.addLogEntry({logString: "logmsg"}));
                            }
    
                        }
                        return response;
    
                    })),
            )
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用combineLatest 运算符。

      sendTestStop = createEffect(() => {
          return this.actions$.pipe(
              ofType('[API] do it'),
              combineLatest(
              [withLatestFrom(this.store.pipe(select(yourSelector))),
                this.RestApiService.DoCommand()]),
              mergeMap(([action, storedata, apiData]) => { buisness logic here } )
        )};
      

      【讨论】:

      • 效果很好,但 tslint 说该方法已被弃用。你能告诉我为什么吗?还请添加您需要来自 rxjs/operators 的 combineLatest
      • 还有,第一个参数是动作,第二个和第三个是值
      • 还有可能给我的restapiservice添加一个参数
      • 抱歉再次编辑,这个答案是错误的,你得到了两个结果,但它只是获取最新的结果,而不是启动新的 RestServiceCall 或 StoreCall,它还在初始化效果时进行调用。
      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2023-02-04
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多