Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:ngrx效果不发射_Angular_Ngrx_Ngrx Effects - Fatal编程技术网

Angular 角度:ngrx效果不发射

Angular 角度:ngrx效果不发射,angular,ngrx,ngrx-effects,Angular,Ngrx,Ngrx Effects,我已经开发了一些Angular应用程序,它们实现了Redux(NgRx)。 我想不出我当前项目的问题 行动: export class GetUserFromStorage implements Action { readonly type = UserActionTypes.GetUserFromStorage; } export class GetUserFromStorageSuccess implements Action { readonly type = UserActio

我已经开发了一些Angular应用程序,它们实现了Redux(NgRx)。 我想不出我当前项目的问题

行动:

export class GetUserFromStorage implements Action {
  readonly type = UserActionTypes.GetUserFromStorage;
}

export class GetUserFromStorageSuccess implements Action {
  readonly type = UserActionTypes.GetUserFromStorageSuccess;
  constructor(public payload: User | null) { }
}

export class GetUserFromStorageFail implements Action {
  readonly type = UserActionTypes.GetUserFromStorageFail;
  constructor(public payload: string) { }
}
减速器:

case UserActionTypes.GetUserFromStorageSuccess:
    return {
        ...state,
        user: action.payload,
        error: ''
    };

case UserActionTypes.GetUserFromStorageFail:
    return {
        ...state,
        error: action.payload
    };
影响:

@Effect() getUserFromStorage$:
Observable<userActions.GetUserFromStorageSuccess | userActions.GetUserFromStorageFail>
    = this.actions$.pipe(
        ofType(userActions.UserActionTypes.GetUserFromStorage),
        tap(() => console.log('GETUserToStorage$', )),
        mergeMap((action: userActions.GetUserFromStorage) =>
            this.storageService.getItem(StorageKeys.USER).pipe(
                map((user: User | null) =>
                    new userActions.GetUserFromStorageSuccess(user)),
                catchError((error: string) =>
                    of(new userActions.GetUserFromStorageFail(error)))
                ))
            );
我已经安装了ReduxDevTools。 GetUserFromStorage操作激发,但不会触发GetUserFromStorage$效果。 “user TEST”的console.logged值显示“user”为“null”。 有什么想法吗?
谢谢。

请确保您确实“激活”了它们

注射效果:

@Injectable()

export class FooEffects {

  @Effect() getUserFromStorage$:
Observable<userActions.GetUserFromStorageSuccess | userActions.GetUserFromStorageFail>
    = this.actions$.pipe(
        ofType(userActions.UserActionTypes.GetUserFromStorage),
        tap(() => console.log('GETUserToStorage$', )),
        mergeMap((action: userActions.GetUserFromStorage) =>
            this.storageService.getItem(StorageKeys.USER).pipe(
                map((user: User | null) =>
                    new userActions.GetUserFromStorageSuccess(user)),
                catchError((error: string) =>
                    of(new userActions.GetUserFromStorageFail(error)))
                ))
            );
}

如果你看到我刚刚用手掌捂住自己的脸…你会哭的。。。非常感谢!在那里:-)很高兴它有帮助!答案仍然适用于ngrx^8.6.0和9。或者
EffectsModule.forFeature()
@Injectable()

export class FooEffects {

  @Effect() getUserFromStorage$:
Observable<userActions.GetUserFromStorageSuccess | userActions.GetUserFromStorageFail>
    = this.actions$.pipe(
        ofType(userActions.UserActionTypes.GetUserFromStorage),
        tap(() => console.log('GETUserToStorage$', )),
        mergeMap((action: userActions.GetUserFromStorage) =>
            this.storageService.getItem(StorageKeys.USER).pipe(
                map((user: User | null) =>
                    new userActions.GetUserFromStorageSuccess(user)),
                catchError((error: string) =>
                    of(new userActions.GetUserFromStorageFail(error)))
                ))
            );
}
import {EffectsModule} from '@ngrx/effects';

imports: [
  EffectsModule.forRoot([
    FooEffects
  ])
]