操作必须具有类型 属性 错误 NgRx

Actions must have a type property error NgRx

我有一个简单的效果 class,它必须从 Firebase 获取一些数据并发送一个新动作。

@Injectable()
export class TripsEffects {

     constructor(private actions$: Actions, private afAuth: AngularFireAuth ) {}

     @Effect()
     loadTrips$ = this.actions$.ofType(TripsActionsTypes.LOAD_TRIPS)
         .pipe(
             switchMap(() => {
                 return this.afAuth.authState.pipe(
                     map(user => new LoadTripsSuccess(user))
                 );
            })
         );
}

但是我收到错误 Actions must have a type property。当用户登录时,他们将被重定向到 Trips 页面,并且在 Trips 组件中我正在调度一个动作来加载行程。这是我的操作:

export enum TripsActionsTypes {
    LOAD_TRIPS = '[Trips] Load Trips',
    LoadTripsSuccess = '[Trips] Load Trips Success',
    LoadTripsFailure = '[Trips] Load Trips Failure'
}

export class  LoadTrips implements Action {
    type: TripsActionsTypes.LOAD_TRIPS;
}

export class  LoadTripsSuccess implements Action {
    type: TripsActionsTypes.LoadTripsSuccess;
    constructor(public payload: any) {}
}

export class  LoadTripsFailure implements Action {
    type: TripsActionsTypes.LoadTripsFailure;
    constructor(public payload: any) {}
}

我认为您需要将操作定义中的 type: ...; 更改为 type = ...;

type: TripsActionsTypes.LOAD_TRIPS; 表示 type 属性 是 TripsActionsTypes.LOAD_TRIPS

类型

type = TripsActionsTypes.LOAD_TRIPS;表示type实例化为TripsActionsTypes.LOAD_TRIPS.

的值

因此您的操作的类型 属性 永远不会被初始化。这是一个很容易犯的错误,因为对象字面量语法使用 : 进行初始化(例如:{ type: 'something' })并且打字稿不会给你一个转译错误。