ngrx 错误“模块“@ngrx/effects”没有导出成员“Effect””

Bas*_*N P 6 javascript mean-stack ngrx angular

我试图在 ngRx 中实现效果。我从“@ngrx/effects”导入 ted“Effect”,但显示错误“模块'“@ngrx/effects”'没有导出成员'Effect'.ts(2305)”

 import { Actions, ofType, Effect } from "@ngrx/effects";
 import { switchMap } from "rxjs/operators";

 import * as AuthActions from './auth.actions';

 type loginResponse = { token: string, message: string, loggedIn: boolean, time: number };

 export class AuthEffects {
    @Effect
    login = this.actions$.pipe(
        ofType(AuthActions.LOGIN),
        switchMap((authData: AuthActions.Login) => {
            return this.http
                .post<loginResponse>(
                    'http://localhost:3000/user/signup',
                    { 
                       email: authData.payload.email, 
                       password: authData.payload.password 
                    }
                )
        })
    )
    constructor(private actions$: Actions, private http: HttpClient) { }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 ngRx 中的效果来实现登录系统。我从“@ngrx/effects”导入了“Effect”,但它显示错误“模块'“@ngrx/effects”'没有导出的成员'Effect'.ts(2305)”

小智 7

看起来您正在尝试使用 @ngrx/effects 库中的 @Effect,但您共享的错误消息显示 @ngrx/effects 模块中没有名为 Effect 的导出成员。

我认为这是因为这个错误是因为您使用的是旧版本的 @ngrx/effects 库。在版本 8 中,Effect 装饰器被修改为 createEffect 函数。

它看起来像这样:

import { Actions, ofType, createEffect } from '@ngrx/effects';
import { switchMap } from 'rxjs/operators';
import { AuthActions } from './auth.actions';

type loginResponse = { token: string, message: string, loggedIn: boolean, time: number };

export class AuthEffects {
  login$ = createEffect(() =>
    this.actions$.pipe(
      ofType(AuthActions.LOGIN),
      switchMap((authData: AuthActions.Login) => {
        return this.http
          .post<loginResponse>(
            'http://localhost:3000/user/signup',
            { 
              email: authData.payload.email, 
              password: authData.payload.password 
            }
          )
      })
    )
  );

  constructor(private actions$: Actions, private http: HttpClient) {}
}
Run Code Online (Sandbox Code Playgroud)