即使Visual Studio代码没有给我任何错误,我还是在Chrome控制台中得到了这个错误。
这是我的interfaces.ts中的代码
export interface Data1{
month: string;
employeeName: string;
date: string;
employmentStatus: string[];
}
这是我在tables.component.ts中的代码
import { Component, OnInit } from '@angular/core';
import { Data1 } from '../../../shared/interfaces';
@Component({
selector: 'app-tables',
templateUrl: './tables.component.html',
styleUrls: ['./tables.component.css']
})
export class TablesComponent implements OnInit {
today = new Date();
dataForTable1: Data1;
months: string[] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
constructor() {
console.log(this.today.getMonth());
console.log(this.months[this.today.getMonth()]);
this.dataForTable1.month = this.months[this.today.getMonth()]
this.dataForTable1.employeeName = '';
this.dataForTable1.date = this.today.getDate().toLocaleString();
this.dataForTable1.employmentStatus = [''];
}
ngOnInit() {
}
}
我尝试使用console.log进行调试,但它给了我正确的输出,但我不知道它为什么说未定义。 我目前正在使用Angular CLI:1.7.4;节点:8.11.1; Typescritp:2.8.1 希望有人可以提供帮助。
答案 0 :(得分:1)
您需要将Data1定义为类而不是接口,或者需要基于该接口定义具体类。然后实例化它,以便您可以使用它的属性。
this.dataForTable1 = new SambleClass()