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

时间:2018-03-28 10:29:55

标签: angular ngrx ngrx-effects

我有simle特效类,必须从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) {}
}

1 个答案:

答案 0 :(得分:6)

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

type: TripsActionsTypes.LOAD_TRIPS;表示type属性类型为TripsActionsTypes.LOAD_TRIPS

type = TripsActionsTypes.LOAD_TRIPS;表示type被实例化为TripsActionsTypes.LOAD_TRIPS的值。

因此,您的操作的type属性永远不会被初始化。这是一个容易犯的错误,因为对象文字语法使用:进行初始化(例如:{ type: 'something' })并且typescript不会为它提供转换错误。