我有一个带有2个菜单项的应用程序;球队,球员.每当我在它们之间切换时,我必须调用不必要的api,即使我存储了数据.
下面是效果文件:
loadTeams$ = createEffect(() => {
return this.actions$.pipe(
ofType(AuthActions.loadTeams),
switchMap((action) =>
this.apiService.teams$.pipe(
tap(() => console.log('get teams request')),
map((teams: Team[]) => AuthActions.loadTeamsSuccess({ teams })),
catchError((error) => {
console.log('err', error);
return of(AuthActions.loadFailure({ error }));
})
)
)
);
});
下面是我调用api的组件
团队名单.组成部分ts
export class TeamsListComponent implements OnInit {
loading$?: Observable<boolean>;
error$?: Observable<string>;
teams$?: Observable<Team[]>;
retrySubject$ = new BehaviorSubject<boolean>(true);
constructor(private store: Store<State>) {}
ngOnInit(): void {
this.store.dispatch(loadTeams());
this.teams$ = this.store.select(getTeams);
this.error$ = this.store.select(getError);
this.loading$ = this.store.select(getLoading);
}
fetchRetry() {
this.retrySubject$.next(false);
}
}
ngOnInit中的这一行会导致不必要的api调用.this.teams$ = this.store.select(getTeams);
如何防止它在初始化应用程序时进行一次api调用?