小编rau*_*oob的帖子

Angular2对象不能设置undefined的属性

所以我在Angular2中有这个小应用程序,我正在尝试定义一个对象.这是主要组成部分.

export class ContactComponent  {
    person: {
    firstname: string;
    lastname: string;
}
constructor(private PeopleService: PeopleService){
}

ngOnInit(){

this.PeopleService.fetchData().subscribe(
    data=> {
        this.person.firstname=data.results[0].name.first;
        console.log(this.person.firstname);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在控制台日志中我得到:

无法设置未定义的属性"firstname"

我无法弄清楚.谢谢.

object undefined angular

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×1

object ×1

undefined ×1