【Angular2】Uncaught (in promise): TypeError: Cannot read pr
背景需要显示用户信息,用到了插值表达式和双向绑定 因为可以正常运行,所以就继续做 问题ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘id’ of undefined TypeError: Cannot read property ‘id’ of undefined 解决1.使用箭头函数 getStudents():void{
this.studentService.getStudents().then(students=>this.students=students);
}
2.在插值表达式中加入问号 {{student?.name}} //not {{student.name}}
说明: 因为在我们加载对象的时候,用的是异步模式,即使promise立刻被处理返回,但是浏览器在开始加载对象的时候,这个对象还是没有定义,所以也就读不到属性 参考: https://stackoverflow.com/questions/38078741/angularjs-2-exception-error-uncaught-in-promise-typeerror-cannot-set-prope (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |