简体   繁体   English

如何在Angular NgRx效果中进行http调用之前调度动作?

[英]How to dispatch an action before making an http call in Angular NgRx effects?

First i want to return the Action Token, after that i want to make an http call where i dispatch another action. 首先,我想返回动作令牌,之后我想发出一个http调用,我发出另一个动作。 I don't know how to return two things. 我不知道如何归还两件事。

@Effect()
  login$ = this.actions$.pipe(
    ofType(AuthActionTypes.LOGIN),
    map(userData => userData.payload),
    exhaustMap((user: any) => {
      return this.userService.login(user.username, user.password)
        .pipe(
            map((res: any) => {
              console.log('authorization' + res.headers.get('Authorization'));
              return new Token({
                token: {
                  token: res.headers.get('Authorization')
                }
              }),
              return this.http
                .get(`${this.restServiceRoot}${this.currentUserRestPath}`)
                .pipe(
                  map((response: any) => {
                    return new LoginSuccess({
                      user: {
                        user: response.name,
                        role: response.role,
                      }
                    });
                  }),
                );
            })
            );
    }),
    catchError(error => of(new LoginFail({error: error})))
  );

There two ways to solve it - 1. 有两种方法可以解决它 - 1。

@Effect
login$ = this.actions$.pipe(
    ofType(AuthActionTypes.LOGIN),
    map(userData => userData.payload),
    exhaustMap((user: any) => {
      return this.userService.login(user.username, user.password)
        .pipe(
            mergeMap((res: any) => {
              console.log('authorization' + res.headers.get('Authorization'));
              this.store.dispatch(new Token({
                token: {
                  token: res.headers.get('Authorization')
                }
              }));              
              return this.http
                .get(`${this.restServiceRoot}${this.currentUserRestPath}`)
                .pipe(
                  map((response: any) => {
                    return new LoginSuccess({
                      user: {
                        user: response.name,
                        role: response.role,
                      }
                    });
                  }),
                );
            })
            );
    }),
    catchError(error => of(new LoginFail({error: error})))
  );

OR In your Login Action you return token action to update the token in your store and one more action to fetch the resource from "${this.restServiceRoot}${this.currentUserRestPath}". 或者在登录操作中,您将返回令牌操作以更新商店中的令牌,并返回另一个操作以从“$ {this.restServiceRoot} $ {this.currentUserRestPath}”获取资源。 In the effect of the second action call your http using "${this.restServiceRoot}${this.currentUserRestPath}" [consider below code as psuedo code - PLEASE NOTE - For some reason code formatting is not working so copy paste the code in your editor]- 在第二个动作的效果中使用“$ {this.restServiceRoot} $ {this.currentUserRestPath}”调用你的http [考虑下面的代码作为伪代码 - 请注意 - 由于某些原因代码格式化不起作用所以复制粘贴代码你的编辑] -

@Effect()
login$ = this.actions$.pipe(
ofType(AuthActionTypes.LOGIN),
    map(userData => userData.payload),
    exhaustMap((user: any) => {
      return this.userService.login(user.username, user.password)
        .pipe(
            mergeMap((res: any) => {
              console.log('authorization' + res.headers.get('Authorization'));
              return [new Token({
                token: {
                  token: res.headers.get('Authorization')
                }
              }), new GetCurrentUserRestPath()]
              )         
            );
    }),
    catchError(error => of(new LoginFail({error: error})))
  );

@Effect()
  login$ = this.actions$.pipe(
    ofType(AuthActionTypes.GET_CURRENT_USER_REST_PATH),    
    switchMap((user: any) => {
      return this.http
      .get(`${this.restServiceRoot}${this.currentUserRestPath}`)
      .pipe(
        map((response: any) => {
          return new LoginSuccess({
            user: {
              user: response.name,
              role: response.role,
            }
          });
        }),
      );
    }),
    catchError(error => of(new LoginFail({error: error})))
  );

Hoe this will give you an idea of how to solve your problem. 锄头这会让你知道如何解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM