Typescript Angular NGRX/Effects actions$.pipe() 未定义

Ula*_*oof 5 typescript ngrx ngrx-effects angular

您好,我有 ngrx/effects 问题 - 管道未定义。下面我附加了示例代码,该代码在编译器中是正确的,但浏览器显示未定义的管道错误。

constructor(
    private actions$: Actions,
    private ethereumService: EthereumService
) { }

loadUser$ = createEffect(() =>
    this.actions$.pipe(
        ofType(loadAccountState),
        mergeMap(() => this.ethereumService.getAccountDetails()
            .pipe(
                map(setAccountStateCompleted),
                catchError(() => EMPTY)
            )
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

应用程序模块:

StoreModule.forRoot(reducers),
EffectsModule.forRoot([AccountEffects]),
Run Code Online (Sandbox Code Playgroud)

编辑:即使这个示例也有相同的错误-_-

logActions$ = createEffect(() =>
    this.actions$.pipe(
        ofType(AccountActions.loadAccountState),
        tap(action => console.log(action))
    ), { dispatch: false });
Run Code Online (Sandbox Code Playgroud)

PS2。我正在使用ActionReducerMap导入到 Root 的主减速器文件作为reducers

import {
  createSelector,
  createFeatureSelector,
  ActionReducerMap,
} from '@ngrx/store';

import * as fromAccount from './account.reducer';

export interface State {
  account: fromAccount.State;
}

export const reducers: ActionReducerMap<State> = {
  account: fromAccount.updateAccountReducer,
};

export const selectAccountState = createFeatureSelector<fromAccount.State>('account');

//Account Selectors
export const selectCurrentUser = createSelector(
  selectAccountState,
  fromAccount.selectActiveAccount
);
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题,请帮忙

Mic*_*ich 11

根据声明方式,$actions当您设置目标 ES2022 或更高版本时,生成的代码可能会发生变化。

快速修复 - 将其添加到您的tsconfig.json

"compilerOptions": {
  {
    // ...
    "useDefineForClassFields": false
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

将来您可能想要重构类初始化并避免使用此标志。

更多信息:

当将编译器目标切换到 ES2022 时,也可能会出现此问题,并且某些 Jest 测试会因此失败。


小智 0

我通过更新 tsconfig.json 文件以引用 ES2020 而不是 ESNEXT 来解决此问题。
tsconfig 键

在此更新之后,在我的效果文件中,我的 createEffect 方法位于构造函数之外,并且它对我有用。