我正在使用 @ngrx/effects 庫創建一個效果服務。但我收到錯誤。似乎錯誤來自actionswitchMap中的引數。如果我懸停它,action:unknown我認為action是ofType操作員的結果。它不能從前一個運算子中推斷出型別嗎?
//auth.effects.ts
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import * as AuthActions from './auth.actions';
import { map, Observable, switchMap } from 'rxjs';
import { ApiService } from 'src/app/services/api.service';
import { Team } from '../models/team.class';
@Injectable()
export class AuthEffects {
constructor(private actions$: Actions, private apiService: ApiService) {}
loadTeams$ = createEffect(() => {
return this.actions$.pipe(
ofType(AuthActions.loadTeams),
switchMap((action) => this.apiService.getTeams()).pipe( // Property 'pipe' does not exist on type 'OperatorFunction<unknown, Team[]>
map((teams: Team[]) => AuthActions.loadTeamsSuccess({ teams }))
)
);
});
}
下面是我的行動。
//auth.actions.ts
import { createAction, props } from '@ngrx/store';
import { Team } from '../models/team.class';
export const loadTeams = createAction('[Auth] Load Teams from Server');
export const loadTeamsSuccess = createAction('[Auth] Load Teams Success', props<{ teams: Team[] }>());
export const loadFailure = createAction('[Auth] Load Failure');
uj5u.com熱心網友回復:
似乎由于語法錯誤而出現錯誤:
...
switchMap((action) => this.apiService.getTeams().pipe(
map((teams: Team[]) => AuthActions.loadTeamsSuccess({ teams }))
))
);
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/465667.html
