繁体   English   中英

无法访问 html 或 typescript angular 中 object 的属性

[英]Can't access property of object in html or typescript angular

export class MyComponent {

    array_all: Array<{ page: any }> = [];
    array_page: Array<{ id: number, name: string }> = [];

    item: Array<{ id: number, name: string }> = [
        {
          id: 1,
          name: 'Test name'
        }
    ]

    constructor(){
        this.array_page.push({
          id: this.item.id,
          name: this.item.name
        });

        this.array_all.push({
            page: this.array_page
        });

        console.log(this.array_all.page.id);
        // error TS2339: Property 'page' does not exist on type '{ page: any; }[]'.
    }
}
<div *ngFor="let page of array_all">
    <h1>Id: {{page.id}}</h1>
    <!--
    error TS2339: Property 'id' does not exist on type '{ page: any; }'
    -->
</div>

我应该在这里做什么才能访问属性 ID 或名称? 当我搜索解决方案时,我看到了一些与将 object 转换为数组然后使用嵌套 *ngFor 相关的内容。 但我不知道该怎么做。

完整代码以备不时之需,这就是我需要先推送到数组然后再推送到其他数组的原因:

export class AboutComponent {

  array_all: Array<{ page: any }> = [];
  array_page: Array<{ id: number, name: string, link: string, image_url: string, image_alt: string }> = [];

    constructor(){
        let start_at: number = 0;
        let last_slice: number = 0;
        for(let i: number = start_at; i < this.knowledge_items.length; i++){
          if(i%2 === 0 && i%3 === 0 && i !== last_slice){
            this.array_all.push({page: this.array_page});
            this.array_page = [];
            this.array_page.push({
              id: this.knowledge_items[i].id,
              name: this.knowledge_items[i].name,
              link: this.knowledge_items[i].link,
              image_url: this.knowledge_items[i].image_url,
              image_alt: this.knowledge_items[i].image_alt
            });
            start_at = i;
            last_slice = i;
          }
          else{
            this.array_page.push({
              id: this.knowledge_items[i].id,
              name: this.knowledge_items[i].name,
              link: this.knowledge_items[i].link,
              image_url: this.knowledge_items[i].image_url,
              image_alt: this.knowledge_items[i].image_alt
            });
            if(i === this.knowledge_items.length - 1){
              this.array_all.push({page: this.array_page});
              this.array_page = [];
            }
          }
        }
        console.log(this.array_all);
    }


    knowledge_items: Array<{id: number, name: string, link: string, image_url: string, image_alt: string }> = [
    {
      id: 1,
      name: 'C++',
      link: 'https://es.wikipedia.org/wiki/C%2B%2B',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
      image_alt: 'C++ programming language'
    },
    {
      id: 2,
      name: 'Python',
      link: 'https://es.wikipedia.org/wiki/Python',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
      image_alt: 'Python programming language'
    },
    {
      id: 3,
      name: 'C++',
      link: 'https://es.wikipedia.org/wiki/C%2B%2B',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
      image_alt: 'C++ programming language'
    },
    {
      id: 4,
      name: 'Python',
      link: 'https://es.wikipedia.org/wiki/Python',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
      image_alt: 'Python programming language'
    },
    {
      id: 5,
      name: 'C++',
      link: 'https://es.wikipedia.org/wiki/C%2B%2B',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
      image_alt: 'C++ programming language'
    },
    {
      id: 6,
      name: 'Python',
      link: 'https://es.wikipedia.org/wiki/Python',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
      image_alt: 'Python programming language'
    },
    {
      id: 7,
      name: 'C++',
      link: 'https://es.wikipedia.org/wiki/C%2B%2B',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/1200px-ISO_C%2B%2B_Logo.svg.png',
      image_alt: 'C++ programming language'
    },
    {
      id: 8,
      name: 'Python',
      link: 'https://es.wikipedia.org/wiki/Python',
      image_url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png',
      image_alt: 'Python programming language'
    }
}

您编写了错误的 HTML 绑定来显示页面 ID。 您的页面 ID 位于array_all.page属性内。 检查 console.log() 以获取更多详细信息。

以下是您的问题的解决方案。

<div *ngFor="let arrayItem of array_all; let i = index">
  <div *ngFor="let item of arrayItem.page">
    <h1>Id: {{ item.id }}</h1>
  </div>
</div>

你的代码是错误的。

检查此链接

文件:

export class MyComponent {

array_all: Array<{ page: any }> = [];
array_page: Array<{ id: number, name: string }> = [];

item: Array<{ id: number, name: string }> = [
    {
      id: 1,
      name: 'Test name'
    }
]

constructor(){
    this.array_page.push({
      id: this.item[0].id,
      name: this.item[0].name
    });

    this.array_all.push({
        page: [...this.array_page]
    });

    console.log(this.array_all[0].page[0].id);
}

}

HTML 档案:

<div *ngFor="let page of array_all">
  <h1>Id: {{page.page.id}}</h1>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM