繁体   English   中英

如何在我的模板中显示获取的数据?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM