我正在使用ng2-completer创建一个搜索框,用户可以在其中搜索github用户名。我已经使用CompleterService,CompleterData从API获取数据。
protected searchStr: string;
protected captain: string;
protected dataService: CompleterData;
constructor(private completerService: CompleterService) {
this.dataService = completerService.remote(null, 'login', 'login');
this.dataService.urlFormater((term: any) => {
return `https://api.github.com/search/users?q=${term}&per_page=5`;
});
this.dataService.dataField('items');
}
<ng2-completer [(ngModel)]="searchStr" [datasource]="dataService" [minSearchLength]="3" inputClass="form-control"></ng2-completer>
ERROR in src/app/search-box/search-box.component.ts(18,22):
error TS2339:
Property 'urlFormater' does not exist on type 'CompleterData'.
src/app/search-box/search-box.component.ts(21,22):
error TS2339: Property
'dataField' does not exist on type 'CompleterData'.
但是开发版本运行良好,但仍然在cmd中产生错误。
答案 0 :(得分:0)
您是否错过了CompleterData的依赖项注入?与其在构造函数之外将其声明为:
protected dataService: CompleterData;
我建议您将其注入:
constructor
(
private completerService : CompleterService,
private dataService : CompleterData
)
{}
答案 1 :(得分:0)
将completerData更改为RemoteData即可。
import { CompleterService, CompleterData, RemoteData } from 'ng2-completer';
searchStr: string;
dataService: RemoteData;
constructor(private completerService: CompleterService) {
this.dataService = completerService.remote(null, 'login', 'login');
this.dataService.urlFormater((term: any) => {
return `https://api.github.com/search/users?q=${term}&per_page=5`;
});
this.dataService.dataField('items');
}
这很好。