Javascript 在传统功能中无法访问此项的引用
我使用Angular 9进行web开发。我想在我的应用程序中实现typeahead特性。因此,我使用ng引导类型。正如下面代码中提到的那样,一切都很完美Javascript 在传统功能中无法访问此项的引用,javascript,angular,typescript,bootstrap-typeahead,Javascript,Angular,Typescript,Bootstrap Typeahead,我使用Angular 9进行web开发。我想在我的应用程序中实现typeahead特性。因此,我使用ng引导类型。正如下面代码中提到的那样,一切都很完美 search = (text$: Observable<string>) => text$.pipe( debounceTime(150), distinctUntilChanged(), switchMap(term => this.GameService.g
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(150),
distinctUntilChanged(),
switchMap(term =>
this.GameService.getCode(term).pipe(
catchError(() => {
return of([]);
}))
),
)
search=(text$:Observable)=>
文本$.pipe(
去BounceTime(150),
distinctUntilChanged(),
开关映射(术语=>
这个.GameService.getCode(术语).pipe(
捕获错误(()=>{
归还([]);
}))
),
)
但不知何故,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([]);
}))
}
),
)
}
search=函数(文本$:可观察){
返回文本$.pipe(
去BounceTime(150),
distinctUntilChanged(),
开关映射(术语=>{
设self=this;//这是未定义的,因此self也是未定义的
返回self.GameService.getCode(term.pipe)(
地图((分辨率)=>{
this.isSearching=false;
返回res;
}),
捕获错误(()=>{
归还([]);
}))
}
),
)
}
如何自定义此代码以支持IE浏览器。在进入
功能之前,请尝试定义let self=this
使用函数表达式时,您失去了代码的作用域和对此
的访问权限,因此您希望在输入第一个函数之前保存此
,为什么不使用transpiler?请注意const self=this
需要超出函数的范围。另外,您的第二个代码段仍在使用箭头函数,并且有一个附加的map()
调用,这在您的原始代码中是不存在的。您能否详细说明一下这一点?不适用于我您的第二个代码段中仍然有一些箭头函数-在switchMap()
、map()
和catchError()
也尝试用传统的函数声明替换switchMap,但仍然没有成功。我不清楚您是替换了所有arrow函数还是仅替换了switchMap()
一个。我尝试了替换所有arrow函数