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

jac*_*ipe 0 ngrx ngrx-effects angular

我有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))
                 );
            })
         );
}
Run Code Online (Sandbox Code Playgroud)

但我得到错误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) {}
}
Run Code Online (Sandbox Code Playgroud)

byg*_*ace 7

我认为你需要从改变type: ...;type = ...;你的操作定义.

type: TripsActionsTypes.LOAD_TRIPS;表示type财产属于类型TripsActionsTypes.LOAD_TRIPS

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

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