ng2-completer-生产版本中产生错误

时间:2018-08-12 08:45:48

标签: angular compiler-errors ng2-completer

我正在使用ng2-completer创建一个搜索框,用户可以在其中搜索github用户名。我已经使用CompleterService,CompleterData从API获取数据。

Component.ts

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');
}

html文件:

 <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中产生错误。

2 个答案:

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

这很好。