问题描述
我正在我的 Angular 应用程序中实现 NGXS,我试图通过路由解析器分派操作。在我当前的实现中,每次路由更改时都会调用相同的 HTTP 请求。
如何防止路由器解析器发出重复的 HTTP 请求?我在状态 isUserDataLoaded
中添加了一个额外的键,但由于我是 NGXS 的新手,我对如何使用它来防止发出重复的 HTTP 请求感到困惑。
Action.ts
export class GetCurrentUser {
static readonly type = '[CurrentUser] Get';
}
State.ts
interface IUserState {
currentUser: ISpotifyUser | null;
isUserDataLoaded: boolean;
}
@State<IUserState>({
name: 'currentUser',defaults: {
currentUser: null,isUserDataLoaded: false
}
})
@Injectable()
export class CurrentUserState {
constructor(private _spotify: SpotifyService) {}
@Selector()
static getCurrentUser(state: IUserState) {
return state.currentUser;
}
@Selector()
static isUserDataLoaded(state: IUserState) {
return state.isUserDataLoaded;
}
@Action(GetCurrentUser)
getUserDetails(ctx: StateContext<IUserState>) {
return this._spotify.getCurrentUser().pipe(
tap((apiResult) => {
ctx.setState({
currentUser: apiResult,isUserDataLoaded: true
});
})
);
}
}
Resolver.ts
resolve(
route: ActivatedRouteSnapshot,state: RouterStateSnapshot
): Observable<any> {
return this._store.dispatch(new GetCurrentUser());
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)