首页 文章

rxjs map运算符在switchMap之后没有在管道中触发

提问于
浏览
1

当谈到rxjs时我觉得自己非常精明,但我无法弄清楚我做错了什么 . 我正在尝试通过将用户登录转换为效果来学习ngrx . 我的效果是正确触发,并从我的服务器返回正确的令牌响应(通过登录控制台验证),但 Map 操作员永远不会在我的效果中被触发 .

这是我的效果代码:

@Injectable()
export class UserEffects {

constructor(
    private authService: AuthService,
    private actions$: Actions
) { }

    @Effect() loginUser$ = this.actions$
    .ofType<userActions.LoginUserAction>(userActions.LOGIN_USER).pipe(
        switchMap(action => this.authService.Login(action.payload)),
        map(user => (new userActions.LoginUserSuccessAction(user)))
    );
}

和我的服务层中的请求:

public Login(model: ILogin) {
return this.http.post<string>(`${baseUrl}api/login`, model).pipe(
  // Returns the object to be stored in AppState
  map(response => this.GetTokenInformation(response))
);

}

我已经尝试过使用flatMap和mergeMap,以防我在ngrx文档中遗漏了一些东西,但这些都没有 .

我的运算符是这样导入的:

import { map, switchMap } from 'rxjs/operators';

让我知道我是否可以提供其他任何东西......谢谢!

1 回答

  • 0

    尝试使用这样的东西:Login Effect from Example App

    loginUser$ = this.actions$.pipe(
      ofType<userActions.LoginUserAction>(userActions.LOGIN_USER),
      map(action => action.payload),
      exhaustMap((auth: any) =>
        this.authService
          .Login(auth)
          .pipe(
            map(user => (new userActions.LoginUserSuccessAction(user)))
          )
      )
    );
    

    switchMap将重置效果,但exhaustMap会等到内部可观察完成 .

    详细了解这两个运营商的差异

相关问题