【发布时间】:2018-03-08 13:37:55
【问题描述】:
我有一个名为行类型 TestEvent 的数组,并且想要推送到数组,我无法输出我推送的对象它只显示未定义 如您所见,this.rows 显示了数组,但是当我尝试输出一个特定的数组 this.rows[0] 时,我得到了未定义。 (打字稿 2.7,Angular 5)
我尝试了以下指南:
- http://robertdunawaypro.blogspot.no/2016/01/018-typescript-arrays-using-interface.html
- TypeScript push not available on interface array
- Typescript: push not available on custom typed array
import { TestEvent } from '../../models/event'
rows: TestEvent[] = []
public push(){
var test: TestEvent = {id: '222', category:'testcat', event_name: 'name'}
console.log(test) // Outputs the array
this.rows.push(test) //Push the array to this.rows
console.log(this.rows) //Outputs array of objects
consloe.log(this.rows[0]) //Outputs undefined
}
Event.ts
export interface TestEvent{
id?: string,
category?: string,
event_name?: string
}
【问题讨论】:
-
首先编写格式化且可以转译的代码。然后查看标记的重复项,您不应该使用
this,因为rows似乎是一个局部变量。 -
我忘了说推送是在方法内部运行的
-
这肯定会影响
this,具体取决于调用方法的方式。this可以与浏览器窗口对象相关联。
标签: angular typescript