期望0类型参数,但得到1-角度7

时间:2019-06-17 06:22:58

标签: angular

我正在使用来自API的Asyng数据创建物料自动完成。 但是这个问题出现了。我在此链接上遵循了“ https://stackblitz.com/edit/angular-material-autocomplete-async2?file=src%2Fapp%2Fuser.class.ts”。并简单地将数据阵列数组的手动填充替换为api机制。

“ this.http.get ”中的IUserResponse用红色下划线标出,错误是“期望的0类型参数,但得到1”

/////// 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; }
  }```

1 个答案:

答案 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) {}