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 (将#修改为@)