如何在加载子组件之前等待http响应?
How to wait for for http response before loading child components?
我的 getSetup() 方法是从 ngOnInit 调用的,它将获取设置子组件(在模板中定义)所需的一些数据。
实际上,通过异步连接,我的子组件将在数据可访问之前创建。所以数据丢失了。
getSetup() {
return this.http.get('http://localhost/apicommand/')
.map((res) => { return res.json(); })
.subscribe(data => { // SETUP CHILD COMPONENT })
.catch((error) => console.log(error));
}
如果你不使用路由器就没有办法 AFAIK。
我会用 *ngIf="data"
将模板包装到 "disable" 组件,直到设置 data
。
我的 getSetup() 方法是从 ngOnInit 调用的,它将获取设置子组件(在模板中定义)所需的一些数据。 实际上,通过异步连接,我的子组件将在数据可访问之前创建。所以数据丢失了。
getSetup() {
return this.http.get('http://localhost/apicommand/')
.map((res) => { return res.json(); })
.subscribe(data => { // SETUP CHILD COMPONENT })
.catch((error) => console.log(error));
}
如果你不使用路由器就没有办法 AFAIK。
我会用 *ngIf="data"
将模板包装到 "disable" 组件,直到设置 data
。