【问题标题】:Angular with ngrx and typescript problems with effects带有ngrx的Angular和带有效果的打字稿问题
【发布时间】:2018-05-07 13:42:45
【问题描述】:

我在使用 ngrx 时遇到了一些问题,我在尝试做一个简单的效果时遇到了错误。我得到的错误是“MonoTypeOperatorFunction<LoadContainerAction>”类型的参数不能分配给 startWith 行上的“OperatorFunction<Action, Action>”类型的参数。

@Injectable()
export class Effects {
    @Effect()
    loadCollection$: Observable<Action> = this.actions$
        .pipe(
            ofType(ActionTypes.LOAD_CONTAINER),
            startWith(new LoadContainerAction()),
            switchMap(() =>
                this.apiService.get<Container[]>("books")
                    .then(items => new LoadContainerSuccessAction(items))
                    .catch(error => of(new LoadContainerFailAction(error)))
            )
        );

    constructor(private actions$: Actions, private apiService: ApiService) { }
}

这就是 ActionTypes 和 LoadContainerAction 的样子:

export enum ActionTypes {
    LOAD_CONTAINER = "[container] load",
    LOAD_CONTAINER_SUCCESS = "[container] load success",
    LOAD_CONTAINER_FAILURE = "[container] load fail",
}

export class LoadContainerAction implements Action {
    readonly type = ActionTypes.LOAD_CONTAINER;
}

这些是我的(相关)进口:

import { Injectable } from "@angular/core";
import { Action } from "@ngrx/store";
import { Effect, Actions, ofType } from "@ngrx/effects";
import { Observable, of } from "rxjs";
import { startWith, switchMap } from "rxjs/operators";

我正在使用 rxjs:6.1.0 和 ngrx:6.0.0-beta.1

【问题讨论】:

    标签: typescript rxjs ngrx


    【解决方案1】:
     ofType<LoadContainerAction>(ActionTypes.LOAD_CONTAINER),
    

    似乎已经解决了,而不是简单地ofType(...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2020-06-13
      • 2020-06-20
      • 2022-10-21
      • 1970-01-01
      相关资源
      最近更新 更多