【发布时间】:2020-10-08 07:36:27
【问题描述】:
我正在使用 Angular 9 进行 Web 开发。我想在我的应用程序中实现 typeahead 功能。所以我正在使用 ng bootstrap typeahead。一切正常,如下面的代码所述。
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(150),
distinctUntilChanged(),
switchMap(term =>
this.GameService.getCode(term).pipe(
catchError(() => {
return of([]);
}))
),
)
但不知何故,这不适用于 IE 浏览器,因为它不支持箭头功能。为了解决这个问题,我用以下方式修改了代码:
search = function(text$: Observable<string>) {
return text$.pipe(
debounceTime(150),
distinctUntilChanged(),
switchMap(term => {
let self = this; // this is undefined and hence self is also undefined
return self.GameService.getCode(term).pipe(
map((res) => {
this.isSearching = false;
return res;
}),
catchError(() => {
return of([]);
}))
}
),
)
}
如何自定义此代码以支持 IE 浏览器。
【问题讨论】:
-
为什么不直接使用转译器?
-
请注意,
const self = this需要位于function的外部。此外,您的第二个 sn-p 仍在使用箭头函数,并且还有一个额外的map()调用,这在您的原始代码中不存在。 -
能否请您详细说明一下
标签: javascript angular typescript bootstrap-typeahead