2

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

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

4

2 回答 2

1

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

这工作正常。

于 2018-08-13T07:12:21.503 回答
0

您是否错过了 CompleterData 的依赖注入?而不是在构造函数之外将其声明为:

protected dataService: CompleterData;

我建议你将它注入到构造函数中:

constructor
(
    private completerService : CompleterService,
    private dataService      : CompleterData
) 
{}
于 2018-08-12T11:31:50.507 回答