首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在模板- Angular8中迭代接口时出错

在模板- Angular8中迭代接口时出错
EN

Stack Overflow用户
提问于 2020-01-13 08:26:12
回答 2查看 686关注 0票数 1

我刚开始学8角。

我有一个小组件,它以一个接口作为输入。接口依次由三个字符串字段组成。

我运行了build命令npm run build:gh-pages,它报告了以下错误:

代码语言:javascript
复制
ERROR in src/app/components/bibliography/bibliography-item/bibliography-item.component.html(1,4): Argument of type 'BibliographicCitation' is not assignable to parameter of type 'Map<unknown, unknown>'.
  Type 'BibliographicCitation' is missing the following properties from type 'Map<unknown, unknown>': clear, delete, forEach, get, and 8 more.

这是我的组件.ts文件:

代码语言:javascript
复制
import { Component, Input } from '@angular/core';
import { BibliographicCitation } from 'src/app/services/xml-parsers/bibliography-parser.service';

@Component({
  selector: 'evt-bibliography-item',
  templateUrl: './bibliography-item.component.html',
  styleUrls: ['./bibliography-item.component.scss']
})

export class BibliographyItemComponent {
  @Input() biblField: BibliographicCitation;
}

这是我的组件.html文件:

代码语言:javascript
复制
<p *ngFor="let biblEl of biblField | keyvalue">
  <em class="biblCitation">
    {{ biblEl.value }}
  </em>
</p>

编辑:

每个biblField输出:

我希望这没什么大不了的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-14 07:54:58

用数组卢克!

代码语言:javascript
复制
import { Component, Input, OnInit } from '@angular/core';
import { BibliographicCitation } from 'src/app/services/xml-parsers/bibliography-parser.service';

export class BibliographyItemComponent implements OnInit {
  @Input() biblField: BibliographicCitation;

  templateBiblCitData;

  ngOnInit() {
    this.templateBiblCitData = Object.values(this.biblField);
  }
}
代码语言:javascript
复制
<p>
  <em *ngFor="let biblEl of templateBiblCitData" class="biblCitation">
    {{ biblEl }}
  </em>
</p>
票数 1
EN

Stack Overflow用户

发布于 2020-05-13 16:35:43

我解决了这个问题,只是让我的自定义接口扩展Map接口。就你而言:

代码语言:javascript
复制
export interface BibliographicCitation extends Map<string, string> {
    key1: string;
    key2: string;
    key3: string;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59712854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档