【问题标题】:I can not call an array of objects我不能调用对象数组
【发布时间】:2019-02-10 08:50:08
【问题描述】:

我无法在我的界面中调用对象数组,我离开了我的 Angular 界面,然后我必须在 typeScript 中的补码中调用它,但是如果没有数组,我不明白如何调用它一个名字,如果我能把它命名,请解释一下!

Vehiculo-interface.ts

export interface vehiculointerface {
    placa?: string,
    [index:number]: { Ubicacion ?: string, Nombre ?: string }
    id?: string
}

detalles-placa.component.ts

import { Component, OnInit } from '@angular/core';
import { PostService } from 'src/app/post.service';
import { ActivatedRoute,Params } from '@angular/router';
import { vehiculointerface } from '../Models/Vehiculo-interface';

@Component({
    selector: 'app-detalles-placa',
    templateUrl: './detalles-placa.component.html',
    styleUrls: ['./detalles-placa.component.css']
})
export class DetallesPlacaComponent implements OnInit {

    constructor(private post: PostService, route: ActivatedRoute) { }

    private vehiculo: vehiculointerface = {
        placa: "",
        { Ubicacion: "", Nombre: ""},

        index() = { Ubicacion: "", Nombre: "" }
    }

    ngOnInit() { }
}

我无法调用在我的接口[index:number]: {Ubicacion ?: string, Nombre ?: string} 中声明的对象数组我无法调用在我的接口Vehiculo-interface.ts 中声明的对象数组这是我在detalles-placa.component.ts 中需要的对象数组

【问题讨论】:

  • 也许其他人可以确认 - 但对我来说,您似乎需要这样的东西... [0]:{Ubicacion: "", Nombre ?: ""} Ubicacion 和 Nombre 都是可选的因为? ..所以你可以有效地用一个,或者另一个,甚至 [0]{} 来代替它。
  • 如果你想要一个对象数组,为什么不使用...数组类型? Array<{Ubicacion?: string, Nombre?: string}>?此外,保持一致并尊重命名约定:类和接口名称以大写字母开头。属性以小写字母开头。
  • @JGFMK 假设这段代码对我有用,但我认为它只给我带来了数组中所有值的一个值,我需要把它全部带来,我对 Angular 来说真的很新,我正在做这个教程:youtube.com/…,在 3:29 分钟,他进行了这个修复,然后在 5:13 调用了该属性,并且不能调用数组,因为它应该是
  • 我真的建议您阅读 Angular 文档 - 英雄示例非常有用。然后看看 Youtube 上的免费 Angular 大学视频。如果您喜欢这样并想了解更多信息,他们会提供更多付费内容。我可以彻底推荐他们。但是现在你想在标记中使用像 *ngFor 这样的东西。 angular.io/tutorialyoutube.com/channel/UC3cEGKhg3OERn-ihVsJcb7A/playlists
  • 我观看了您发布的视频链接,但没有看到任何看起来像您的索引属性的内容??

标签: angular typescript angular7 loopback


【解决方案1】:

这个语法:

[index:number]: { Ubicacion ?: string, Nombre ?: string }

是定义可索引类型的索引签名。而可索引类型是我们可以“索引”的类型。

您可以在此处阅读有关它们的更多信息:https://www.typescriptlang.org/docs/handbook/interfaces.html

这是文档中的示例:

interface StringArray {
    [index: number]: string;
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

let myStr: string = myArray[0];

所以它不是定义一个特定的命名属性,而是提供一种“索引”你的类型的方法。

单独使用索引器

有了你的结构,这对我有用:

  private vehiculo: vehiculointerface = [{
    Ubicacion: "some string",
    Nombre: "some id"
  },
  {
    Ubicacion: "other string",
    Nombre: "other id"
  }]

我将它视为一个对象数组。 (不知道为什么要为不打算作为数组处理的信息设置索引器属性?)

然后您可以在模板中访问它,如下所示:

<div *ngFor="let item of vehiculo">
  {{ item.Ubicacion}}; {{ item.Nombre}}
</div>

与其他成员一起使用索引器

我并没有真正与其他成员一起使用索引器,所以我不确定你想要达到什么目的。但是像这样的东西对我有用:

  private vehiculo: vehiculointerface = {
    placa: "some string",
    id: "some id"
  }

  ngOnInit() {
    this.vehiculo["some index"] = {
      Ubicacion: "some string",
      Nombre: "some id"
    }
    this.vehiculo["other index"] = {
      Ubicacion: "other string",
      Nombre: "other id"
    }
    console.log(JSON.stringify(this.vehiculo));
  }

注意非索引器成员是如何独立于索引器定义的。

上面的代码定义了接口的基本属性,然后使用索引器设置附加值。

生成的结构如下所示:

{
 "placa":"some string",
 "id":"some id",
 "some index":{"Ubicacion":"some string","Nombre":"some id"},
 "other index":{"Ubicacion":"other string","Nombre":"other id"}
}

然后我使用以下代码在模板中访问它:

<div *ngFor="let item of vehiculo2 | keyvalue">
  {{ item.key}}; {{ item.value}}
</div>

但请注意,索引属性的值显示为[object, Object],因为它们的类型为{ Ubicacion ?: string, Nombre ?: string }

这是一个堆栈闪电战:https://stackblitz.com/edit/angular-indexer-deborahk

【讨论】:

  • 事实记得,通过 API REST 查阅信息,因此......它对我不起作用
猜你喜欢
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 2021-05-03
  • 2020-08-09
  • 2016-08-28
  • 1970-01-01
相关资源
最近更新 更多