NgRx效果:我做错了什么?

问题描述 投票:0回答:1

因此,当我对/api/auth/login进行API调用时,我正在尝试根据从服务器获取的用户类型调度不同的操作数组。

@Effect()
  login = this.actions$.pipe(
    ofType(AuthActions.ActionTypes.TryLogin),
    map((action: AuthActions.TryLogin) => {
      return action.payload;
    }),
    switchMap(loginData => {
      return this.httpClient.post<{
        jwtToken: string;
        usertype: string;
        expiresIn: number;
        userId: string;
      }>(`${this.BACKEND_URL}/api/auth/login`, loginData);
    }),
    mergeMap(res => {
      const actions = [
        {
          type: AuthActions.ActionTypes.Login
        },
        {
          type: AuthActions.ActionTypes.SetToken,
          payload: res.jwtToken
        },
        {
          type: AuthActions.ActionTypes.SetTokenExpiry,
          payload: res.expiresIn
        }
      ];
      if (res.usertype === 'Admin') {
        this.router.navigate(['/admin-panel']);

        return [...actions, {type: AdminActions.ActionTypes.SetId, payload: res.userId}];
      } else {
        this.router.navigate(['/client-panel']);

        return [...actions, {type: UserActions.ActionTypes.SetId, payload: res.userId}];

      }
    })
  );

但我得到这个错误:

Argument of type '(res: { jwtToken: string; usertype: string; expiresIn: number; userId: string; }) => ({ type: Act...' is not assignable to parameter of type '(value: { jwtToken: string; usertype: string; expiresIn: number; userId: string; }, index: number...'.
  Type '({ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: ...' is not assignable to type 'ObservableInput<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: strin...'.
    Type '({ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: ...' is not assignable to type 'ObservableInput<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: strin...'.
      Type '({ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: ...' is not assignable to type 'Iterable<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | ...'.
        Types of property '[Symbol.iterator]' are incompatible.
          Type '() => IterableIterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload...' is not assignable to type '() => Iterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string...'.
            Type 'IterableIterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: stri...' is not assignable to type 'Iterator<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | ...'.
              Types of property 'next' are incompatible.
                Type '{ (value?: any): IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes...' is not assignable to type '{ (value?: any): IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes...'. Two different types with this name exist, but they are unrelated.
                  Type 'IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string...' is not assignable to type 'IteratorResult<{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string...'. Two different types with this name exist, but they are unrelated.
                    Type '{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: A...' is not assignable to type '{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: A...'. Two different types with this name exist, but they are unrelated.
                      Type '{ type: ActionTypes; payload: string; }' is not assignable to type '{ type: ActionTypes; payload?: undefined; } | { type: ActionTypes; payload: string; } | { type: A...'.
                        Type '{ type: ActionTypes; payload: string; }' is not assignable to type '{ type: ActionTypes; payload: string; }'. Two different types with this name exist, but they are unrelated.
                          Types of property 'type' are incompatible.
                            Type 'ActionTypes' is not assignable to type 'ActionTypes'. Two different types with this name exist, but they are unrelated.

如果我将管理员登录和用户登录分开,它可以正常工作。但这意味着大部分代码都是相同的。任何人都可以帮助我吗?

angular ngrx rxjs6 ngrx-effects
1个回答
1
投票

我认为唯一的方法是键入是一个Action

if (res.usertype === 'Admin') {
        this.router.navigate(['/admin-panel']);

        return [...actions, {type: AdminActions.ActionTypes.SetId, payload: res.userId} as Action];
} else {
        this.router.navigate(['/client-panel']);

        return [...actions, {type: UserActions.ActionTypes.SetId, payload: res.userId} as Action];
}

© www.soinside.com 2019 - 2024. All rights reserved.