angular - ngOnInit() 被调用两次并获得两个 API 响应
问题描述
app.component.ts
export class AppComponent implements OnInit{
title = "quogen";
response : Response[];
constructor (private dataService : DataService) {
}
ngOnInit () {
console.log("calling init");
return this.dataService.getQuote().subscribe(data => this.response = data);
}
}
app.component.html
<div *ngIf="response">
<p>{{ response['content'] }}</p>
<p>{{ response['author'] }}</p>
</div>
<router-outlet></router-outlet>
ngOnInit()
据我所知,该 API 被调用了两次。为什么会这样,我该如何解决这个问题?
解决方案
推荐阅读
- python - 如何从 json 文件的依赖项中读取 python 工具并在 python 解包时安装它?
- c# - IIS Express Asp.Net 长 URL 返回 400 Bad Request
- wordpress - Wordpress 中的恶意代码(恶意重定向)
- c# - 如何在文本文件中写入格式化字符串
- python - 使用Python运行时如何改变视频的运行速度
- python - 获取DF中最大值的索引名称
- identityserver4 - 循环 ReturnUrlParsers 时的空引用
- jquery - 当我关闭所有组行时,DataTable 显示 0 条记录?
- python - 如何使用 Gtk.events_pending?
- android - Error inflating class android.support.design.widget.AppBarLayout on Samsung Galaxy Note Edge