![](/img/trans.png)
[英]Data fetched in ngOnInit does not display until I resize my browser
[英]How can I display the fetched data in my template?
我创建了一个名为product.ts
的文件,然后将这些代码添加到其中:
export interface Product
{
id:number;
}
零件:
export class AppComponent {
title = 'angpr';
todo:Product;
constructor(public api: ApiService) {
}
ngOnInit() {
this.api.getApi()
.subscribe(
data => this.todo=data
);}
}
导入接口并使用todo:Product;
在上面的代码中,我得到了这个错误: Property 'todo' has no initializer and is not definitely assigned in the constructor.
使用界面的目标是使用{{todo.id}}
在我的模板中显示获取的数据。
如何在我的模板中显示获取的数据?
我该如何解决这个错误: Property 'todo' has no initializer and is not definitely assigned in the constructor.
?
将其声明为可选,因为它可以是可选的。
todo?:Product;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.