angular - 如何定义接口的对象数组?我的数据:数组=[]; 我得到属性 'xxxxxx' 在类型 'myInterface[]' 上不存在
问题描述
我正在尝试为对象数组定义一个接口,我希望组件中的初始值是一个空数组
我在文件中有一个接口:
export interface myInterface{
"picture": string,
"lastUpdated": Date
}
在我的组件中,我导入了这个接口:
import {myInterface} from "./interfaces/myInterface"
.
.
myData:Array<myInterface>=[]; //default [] initial value
.
.
//I get the data by a rest api
then((data) => {
this.myData=data;
this.myData.lastUpdated= new Date(); //Property 'lastUpdated' does not exist on type 'myInterface[]'
})
我收到此错误:
this.myData.lastUpdated= new Date(); //Property 'lastUpdated' does not exist on type
如何解决?
解决方案
我不确定您是想要它们的第一个值lastUpdated
还是全部。
myData
是一个对象数组,你应该遍历它以处理它的所有值
myData: MyInterface[] = []; // EMPTY array of type MyInterface
myLastUpdatedArray: any = []; // EMPTY array of type any
...
then((data) => {
this.myData = data;
const aVariable = this.myData[0].lastUpdate; // aVariable will hold your .lastUpdated value
// if you want all of them
myData.forEach((data) => this.myLastUpdatedArray.push(data.lastUpdated));
})
推荐阅读
- c - fwrite() 是否会在多次调用时覆盖?
- java - 除了 JDBC,还有哪些不同的方法可以使用 Java 连接到 Hive?
- cassandra - ResponseError:没有足够的副本可用于一致性序列查询(需要 2 个但只有 1 个活动)
- c# - 实体框架上的 LINQ-to-Entities 查询
- apache-kafka - 当记录处理时间超过“max.poll.interval.ms”时,记录/消息在消费期间会发生什么?
- java - 如何在 Android Studio 中更改 3D 对象的颜色?
- python-3.x - 单击按钮时更新 tkinter 画布小部件
- java - 使用正则表达式或 Unicode 字符拆分字符串?
- vue.js - vue:为什么需要事件系统标记,而函数可以作为道具传递
- mysql - 是否应该使用 USE/FORCE INDEX 更改 MySQL 查询中的 EXPLAIN 输出?