类型 'void | Observable<any>' 不能分配给类型"可观察输入<any>"。类型 'void' 不可分配给类型"可观察输入<any>"



我在visual studio代码中得到了这个错误:

参数类型'(query: string) =>void | Observable'不是可赋值给类型为'(value: string, index: number) =>ObservableInput"。类型'void | Observable'则不是可赋值给'ObservableInput'类型。'void'类型不能赋值给'ObservableInput'类型

校验码:

public searchVal(val: string) {
this.suggestions = (new Observable((observer: Observer<string>) => {
observer.next(val);
})).pipe(
switchMap((query: string) => {
if (query) { 
return this.returnApiCall(query);  //THIS IS WORK WITH ONLY ONE RETURN
}
return of([]);
})
);
}

public searchVal(val: string) {
this.suggestions = (new Observable((observer: Observer<string>) => {
observer.next(val);
})).pipe(
switchMap((query: string) => {
if (query) { 
if(this.serverApi === 'Driver'){
return this.getAllDrivers(query);
}else if(this.serverApi === 'Vehicle'){
return this.getVehicle(query);
}
}
return of([]);
})
);
}

当我尝试返回一些其他api调用不工作?

我的api调用是:
returnApiCal(query: string) {
return this.accountsService.getDrivers(query)
.pipe(
map((data: any) => {  
return data.body || [];
}),
tap(() => noop, err => {
this.errorMessage = err && err.message || 'Something goes wrong';
})
)
}

这一行是红线:

switchMap((query: string) => { //here

确保switchMap的回调函数总是返回一些东西:

public searchVal(val: string) {
this.suggestions = (new Observable((observer: Observer<string>) => {
observer.next(val);
})).pipe(
switchMap((query: string) => {
if (query) { 
if(this.serverApi === 'Driver'){
return this.getAllDrivers(query);
}else if(this.serverApi === 'Vehicle'){
return this.getVehicle(query);
}
else { // you need to provide else here
// return an observable when the two above conditions were not satisfied
}
}
return of([]);
})
);
}

相关内容

  • 没有找到相关文章

最新更新