微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

switchMap 仅取消某些端点

如何解决switchMap 仅取消某些端点

我有一个表单控件输入来搜索结果

component.html

<input matInput [formControl]="input">

我正在使用 valueChanges observable 检查更改。

component.ts

this.xx = this.input.valueChanges.pipe(
  switchMap(res => {
    return this.someService.test(res);
  }),);

我不明白的是,在快速输入时取消先前的 http 请求仅适用于某些端点。

someService.ts

public test(q: string): Observable<any> {
  // api-1 cancelling
  return this.http.get<any>('http://localhost:8080/api/geosuggestion?q=' + q);
  // api-2 not cancelling
  // return this.http.get<any>('http://localhost:8080/api/items?q=' + q,{ headers: { 'auth-token': this.authService.getToken() } });
}

相似的问题总是源于他们使用了不同的可观察链。在我的情况下,必须有不同的原因,因为我在两种情况下都使用完全相同的逻辑,并且只替换端点 url。

api-1 (successfully cancelling)

api-1 screenshot

api-2 (not cancelling)

api-2 screenshot

为什么 switchMap 取消仅适用于我的某些端点?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。