简体   繁体   English

如何将两个不同的状态从我的 ngrx 效果传递到我的服务功能?

[英]How do I pass two different pieces of state from my ngrx effect to my service function?

I have an effects function where I'm trying to pass data from my dispatched action as well as a separate selector to a function in a service, but I'm getting lost in the RXJS syntax.我有一个效果函数,我试图将数据从我的调度操作以及一个单独的选择器传递给服务中的函数,但我在 RXJS 语法中迷失了方向。 I'm pretty sure I'm not mapping the data correctly.我很确定我没有正确映射数据。 Please see the relevant code.请查看相关代码。

// effects // 效果

 updateCohort$ = createEffect(() =>
    this.actions$.pipe(
        ofType(getCohortActions.updateActiveCohort.type),
        withLatestFrom(this.store.pipe(select(activeCohort))),
        map(([action, selector ]) => (
            this.cohortFeaturesServices.updateCohorts(action, selector)
        )),

// service // 服务

public updateCohorts(action, selector): Observable<any> {
    const url = `api/services/cohorts/frontend/`;

    return this.http.patch(`${url}/${selector.id}/`, action.changes);
}

Visual Studio Code underlines the entire function and I get the following error in my console. Visual Studio Code 强调了整个函数,我在控制台中收到以下错误。

Type 'Observable>' is not assignable to type 'Observable'.类型 'Observable>' 不可分配给类型 'Observable'。 Property 'type' is missing in type 'Observable' but required in type 'Action'. “Observable”类型中缺少属性“type”,但“Action”类型中需要。

How can I fix my effect and successfully pass my action and my selector to my service call?如何修复我的效果并将我的操作和我的选择器成功传递给我的服务调用?

You need to use switchMap/mergeMap/concatMap/exhaustMap to "unbox" observable coming from HTTP service -> result of switchMap -> map into action such as your effect must return actions(such as it is just a default effect with action dispatch).您需要使用switchMap/mergeMap/concatMap/exhaustMap来“拆箱”来自 HTTP 服务的switchMap -> switchMap结果 -> map动作,例如您的效果必须返回动作(例如它只是带有动作调度的默认效果) .

Also, I suggest that the API method would have the changes as parameters, not an action.另外,我建议 API 方法将更改作为参数,而不是操作。

updateCohort$ = createEffect(() =>
  this.actions$.pipe(
    ofType(getCohortActions.updateActiveCohort.type),
    withLatestFrom(this.store.pipe(select(activeCohort))),
    switchMap(([action, cohort]) =>
      this.cohortFeaturesServices.updateCohorts(action.changes, cohort)
    ),
    map((result: CohortUpdateResponse) => successCohortUpdateAction())
  )
)


public updateCohorts(changes: Partial<Cohort>, cohort: Cohort): Observable<CohortUpdateResponse> {
  const url = `api/services/cohorts/frontend/`;

  return this.http.patch(`${url}/${cohort.id}/`, changes);
}

Something like this.像这样的东西。

PS added some "invented" types to show what is going on and where PS 添加了一些“发明的”类型来显示正在发生的事情和位置

PSS didn't check for typos in code, such wrote answer right in answer window PSS 没有检查代码中的拼写错误,例如在答案窗口中写下答案

GL :) GL :)

For anyone else they may come across the same issue I figured it out.对于其他任何人,他们可能会遇到同样的问题,我想通了。 I wasn't returning an action.我没有返回一个动作。

updateCohort$ = createEffect(() =>
    this.actions$.pipe(
        ofType(getCohortActions.updateActiveCohort.type),
        withLatestFrom(this.store.pipe(select(activeCohort))),
        exhaustMap(([action, cohort]) => this.cohortFeaturesServices.updateCohorts(action, cohort).pipe(

        )),
        map((response) => ({
            type: getCohortActions.updateActiveCohortSuccess.type, // success action
            success: response
        })),
        catchError((err) => of(getCohortActions.updateActiveCohortError(err))) // error action
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的 ngrx/data 服务中的数据自动刷新我的 PrimeNG 表? - How do I automatically refresh my PrimeNG table from data from my ngrx/data service? 如何从Ngrx存储中获取值到模板中? - How do I get the value from an ngrx store into my template? 我如何编写NgRx 8效果来触发诺言中的动作 - How do i write an NgRx 8 Effect that triggers an action from a promise NGRX - 如何在 angular 防护中访问我的应用程序 state? - NGRX - How can I access my app state in angular guard? 为什么我的@Effect用Angular 6中的ngrx覆盖现有状态? - Why is my @Effect overwriting existing state with ngrx in Angular 6? 如何创建组合来自两个不同功能模块的数据的 NGRX 选择器? - How do I create a NGRX selector that combines data from two different feature modules? NGRX:如何从内部效果使用不同的有效载荷多次调用相同的服务 - NGRX: How to call same service multiple time with different payload from inside effect 如何从组件中获取服务中的Observable的值并将其传递给我的视图? - How do I get the value of an Observable in a service from a component and pass it to my view? 在 NGRX 中,我如何链接来自不同减速器的动作? - In NGRX, how do I chain actions from different reducers? 如何从回调函数使用ngrx / angular5从效果返回? - How can I return from an effect with ngrx / angular5 from a callback function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM