-1

我在 Angular 7 上并不断收到以下错误:

错误:(22, 51) TS2339:“操作”类型上不存在属性“ofType”。

同时遵循本教程:https ://angularfirebase.com/lessons/ngrx-with-firebase-auth-google-oauth-login/ 。

对此功能的支持是否发生了变化?

  @Effect()
  googleSignIn: Observable<Action> = this.actions.ofType(authActions.GOOGLE_LOGIN)
    .map((action: authActions.GoogleLogin) => action.payload)
    .switchMap(payload => {
      return Observable.fromPromise(this.authService.googleSignIn());
    })
    .catch(err => {
      return Observable.of(new authActions.AuthError({ error: err.message }));
    });

更新

我已将代码更新为:

  @Effect()
  googleSignIn: Observable<Action> = this.actions.pipe(
    ofType(authActions.GoogleSignIn),
    map((action: authActions.GoogleSignIn) => action.payload),
    switchMap(payload => {
      return Observable.fromPromise(this.authService.googleSignIn());
    }),
    catchError(err => {
      return Observable.of(new authActions.AuthError({ error: err.message }));
    }),
  );

修复了上面的错误,但现在我收到了这条消息:

src/app/modules/auth/auth.effects.ts(20,12): error TS2345: Argument of type 'type
of GoogleSignIn' is not assignable to parameter of type 'string | ActionCreator<s
tring, FunctionWithParametersType<any[], object>>'.
  Type 'typeof GoogleSignIn' is not assignable to type 'ActionCreator<string, Fun
ctionWithParametersType<any[], object>>'.
    Type 'typeof GoogleSignIn' is not assignable to type 'FunctionWithParametersT
ype<any[], object>'.
      Type 'typeof GoogleSignIn' provides no match for the signature '(...args: any[]): object'.

auth.reducer.ts 的内容:

import { User } from '../shared/models/user';
import * as authActions from './auth.actions';

const defaultUser = new User(null, 'GUEST');

export type Action = authActions.All;

export function authReducer(state: User = defaultUser, action: Action) {
  switch (action.type) {
    case authActions.GET_USER:
      return { ...state };

    case authActions.AUTHENTICATED:
      return { ...state, ...action.payload };

    case authActions.NOT_AUTHENTICATED:
      return { ...state, ...defaultUser};

    case authActions.GOOGLE_SIGNIN:
      return { ...state, loading: true };

    case authActions.AUTH_ERROR:
      return { ...state, ...action.payload, loading: false };

    case authActions.LOGOUT:
      return { ...state, loading: true };
  }
}
4

1 回答 1

1

从 RxJs 6.x 开始,你应该这样写:

@Effect()
  googleSignIn: Observable<Action> = this.actions.pipe( 
    ofType(authActions.GOOGLE_LOGIN),
    map((action: authActions.GoogleLogin) => action.payload),
    switchMap(payload => {
      return Observable.fromPromise(this.authService.googleSignIn());
    }),
    catchError(err => {
      return Observable.of(new authActions.AuthError({ error: err.message }));
    }),
  );
于 2019-08-19T00:36:22.003 回答