类型'Statement'不能分配给'any []'

时间:2017-10-16 19:17:18

标签: angular

我无法弄清楚为什么我会一直收到此错误:

C:/Users/zuberiaz/Documents/ECT/ECT2/ECT2/src/app/main-panels/result-display-panel/record-display/record-display.component.ts (89,103): Argument of type '(statements: Statement[]) => void' is not assignable to parameter of type '(value: Statement) => void'.

参数“声明”和“值”的类型不兼容。     类型'Statement'不能分配给'Statement []'类型。       “声明”类型中缺少属性“包含”。

这是我的组件文件:

statements: any[];
ngOnInit() {
    this.recordDisplayService.getStatementsBySource('8d71e58c-e56d-4782-b695-8d6bb87ace5e').subscribe(statements => {this.statements = statements;});
    this.politicalParties = [];
}

服务档案:

//Fetch statements by source id
getStatementsByUrl = 'http://api/V1/Statement/QueryDocument?properties=all';
getStatementsBySource(sourceId: string): Observable<Statement> {
  return this.http.get(this.getStatementsByUrl + '/' + sourceId)
  .map(data =>  {return this.extractData(data);})
  .catch(this.handleError);
}

1 个答案:

答案 0 :(得分:1)

您的服务类:

//Fetch statements by source id
getStatementsByUrl = 'http://api/V1/Statement/QueryDocument?properties=all';
getStatementsBySource(sourceId: string): Observable<Statement> {
  return this.http.get(this.getStatementsByUrl + '/' + sourceId)
  .map(data =>  {return this.extractData(data);})
  .catch(this.handleError);
}

似乎要返回一个Statement。如果您要获取多个语句,则将返回类型更改为:

getStatementsBySource(sourceId: string): Observable<Statement[]>

如果它确实返回了一个语句,那么您需要更改组件代码:

statement: Statement;
ngOnInit() {
    this.recordDisplayService.getStatementsBySource('8d71e58c-e56d-4782-b695-8d6bb87ace5e').subscribe(statement => {this.statement = statement;});
    this.politicalParties = [];
}