问题描述
我在我的应用中使用 ngrx/data。
我有一个非常快的申请。 你可以在 UI 上选择一些东西,我需要更新我的视图。用户可以非常快速地完成此操作,在这种情况下,我不需要来自先前请求的任何先前数据。
但是当我添加 switchMap 时 - 我的请求不会取消。
interface User {
id: number;
name: string;
}
@Component({
selector: 'nrwl-test-root',templateUrl: './app.component.html',styleUrls: ['./app.component.scss'],})
export class AppComponent implements OnInit {
test$ = new Subject();
userEntity: EntityCollectionService<User>;
constructor(private http: HttpClient,entityDeFinition: EntityDeFinitionService,entityServices: EntityServices) {
entityDeFinition.registerMetadataMap({
User: {},});
this.userEntity = entityServices.getEntityCollectionService<User>('User');
}
ngOnInit() {
this.test$.pipe(
switchMap(() => {
// return this.http.get('api/user') // switchMap works here
return this.userEntity.getAll() // switchMap doesn't work here
})
).subscribe();
}
onClick() {
this.test$.next(Math.random());
}
}
我有“@ngrx/data”:^0.10.2
但我在最新版本 11.0.1 上检查它 - 我有相同的行为。
我找到了一种解决方法,只需将 httpClient 用于它,并在成功请求后更新我的 entityCache。但我不喜欢这种方法。
据我所知,实体服务总是返回 AnonymousSubject 而不是 Observable。 这也是我无法取消请求的原因。但也许我可以以正确的方式做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)