【问题标题】:Possible return types for an ArrayList in Angular6? Do invalid return types cause the .Subscribe function to be undefined?Angular6 中 ArrayList 的可能返回类型?无效的返回类型会导致 .Subscribe 函数未定义吗?
【发布时间】:2019-03-25 14:59:02
【问题描述】:

在成功调用 API 后,.subscribe 承诺未定义。 API 像这样返回一个 JSON 数组列表;

[
    {
        "id": 1,
        "matches": [
        {
            "prop1": "string,
            "prop2": "string,
        },
        {
            "prop1": "string,
            "prop2": "string,
        },
    }
]

.ts 文件请求返回类型为Fixture[],Fixture 对象包含一个对象数组。

export class FixtureComponent {
  public fixtures: Fixture[];

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {

    http.get<Fixture[]>(baseUrl + 'api/Fixture/').subscribe(result => {
      this.fixtures = result;
    }, error => console.error(error));
  }
}

interface Fixture {
  id: number;
  match: Match;
  matches: Array<Match>;
}

interface Match {
  id: number,
  homeClub: string;
  awayClub: string;
  date: Date;
}

我认为问题出在 Fixture[] 不是合适的类型,但是当我尝试将其更改为更合适的类型定义时,出现索引错误,就好像我在尝试使用另一个对象来索引数组。

我希望返回一个数组列表,我将在 DOM 中使用嵌套的 *NgFor 显示该列表。

感谢任何帮助。谢谢。

编辑:

result 中的错误:输入意外结束。

【问题讨论】:

  • 这感觉像是一个 A/B 问题,您试图在 API 调用返回之前呈现 fixtures。产生的实际错误是什么?
  • 没有实际错误,首先出现问题的地方是subscribeis undefined。控制台很清晰,error 也为空。在我更改中间层的夹具模型的布局之前,我有数据通过。
  • 结果产生语法错误:unexpected end of input.
  • 服务器响应是否正确形成?

标签: html arraylist angular6 ngfor


【解决方案1】:

只需在 component.ts 中的所有实例中将 Fixture[] 更改为 Fixture

运作良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多