请帮我做一个在 Angular2 中同步 http 的例子?
我试过如下: 在组件中:
getAllAddress(){
this.addressService.getAllAddress().then(
result => {
this.data = result.list;
this.onChangeTable(this.config, null);
console.log('FIRST');
}
);
console.log('LAST');
}
在役:
public getAllAddress(){
return this.__http.get('LOCATION')
.map((res) => {
return res.json()
})
.toPromise();
}
但是控制台显示日志在“FIRST”之前是“LAST”。
谢谢。
最佳答案
您必须创建自己的实现 Connection
和 ConnectionBackend
类,并在引导您的应用程序时注入(inject)它。请参阅下面的示例代码
export class XHRSynchronousConnection implements Connection
{
}
export class XHRSynchronousConnectionBackend implements ConnectionBackend
{
}
您可以按如下方式引导它
bootstrap([provide(ConnectionBackend, {useClass:XHRSynchronousBackend}),
provide(Connection,{useClass:XHRSynchronousConnection}];
您可以在 actual source code 中查看其余代码.
关于Angular2 http 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505531/