我有一个HospitalComponent试图显示医院列表的组件.它使用的readAll方法HospitalService(从firebase返回一个Observable):
ngOnInit() {
this.hospitalService
.readAll() // Make a firebase call
.subscribe(hospitals => this.hospitals = hospitals);
}
Run Code Online (Sandbox Code Playgroud)
路由/hospital已插入此组件HospitalComponent.每次我到达时/hospital,Angular 2都会创建一个新的HospitalComponent,并且新的呼叫将被发送给hospitalService.
每次到达时/hospital,医院名单都会延迟显示.
从服务构造函数本身检索医院列表是一种好习惯吗?这样,我可以从后台管理刷新列表,而不是有一些延迟.我会在组件中:
ngOnInit() {
this.hospitals = this.hospitalService.readAll();
}
Run Code Online (Sandbox Code Playgroud)
并在服务中:
constructor(private hospitalService: HospitalService) {
hospitalService.subscribe(hospitals => this.hospitals = hospitals);
}
Run Code Online (Sandbox Code Playgroud)
但这意味着要手动管理所有医院的变化.