我正在使用来自API的Asyng数据创建物料自动完成。 但是这个问题出现了。我在此链接上遵循了“ https://stackblitz.com/edit/angular-material-autocomplete-async2?file=src%2Fapp%2Fuser.class.ts”。并简单地将数据阵列数组的手动填充替换为api机制。
“ this.http.get
/////// app.service .ts
search(filter: {companyName: string} = {companyName: ''}): Observable<IUserResponse> {
return this.http.get<IUserResponse>(this.api+'PublicAPI/Schools',this.option)
.pipe(
tap((response: IUserResponse) => {
response.results = response.results
.map(user => new Schoollist(user.companyID, user.companyName))
.filter(user => user.companyName.includes(filter.companyName))
return response;
})
);
}
///// schoollist.class.ts
export class Schoollist {
constructor(public companyID: string, public companyName: string) {}
}
export interface IUserResponse {
total: string;
results: Schoollist[];
}
/// app.component.ts
filteredSchs: Schoollist[] = [];
usersForm: FormGroup;
isLoading = false;
constructor(public dialogRef: MatDialogRef<EducationalbackgroundUpdateComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
public dialog: MatDialog,
private global: GlobalService,
private http: Http,
private fb: FormBuilder,
) {
}
ngOnInit() {
this.usersForm = this.fb.group({
userInput: null
})
this.usersForm
.get('userInput')
.valueChanges
.pipe(
debounceTime(300),
tap(() => this.isLoading = true),
switchMap(value => this.global.search({companyName: value})
.pipe(
finalize(() => this.isLoading = false),
)
)
)
.subscribe(users => this.filteredSchs = users.results);
}
displayFn(sch: Schoollist) {
if (sch) { return sch.companyName; }
}```
答案 0 :(得分:0)
您可能正在使用Http
而不是HttpClient
?如果您使用的是HttpClient,则get()
应该允许类型参数,而Http
则不允许类型参数。
来源:typescript error Expected 0 type arguments for get call
请确保您导入:import { HttpClient } from '@angular/common/http';
并将其放入构造函数中:constructor(private http: HttpClient) {}