【发布时间】:2018-02-20 15:10:19
【问题描述】:
使用 Angular 5。组件调用服务。在进行服务器调用之前,该服务必须调用另一个服务。我无法在组件中异步获取结果。为了简洁起见,我在下面使用省略号。
组件:
...
import { SystemOnlineService } from '../services/system-online.service';
...
constructor(private sys: SystemOnlineService) {
sys.getStatus()
.then(result => {
console.log(result);
});
}
系统在线服务:
import { Injectable } from '@angular/core';
import { Wakanda } from './wakanda.service';
import 'rxjs/add/operator/toPromise';
...
getStatus() {
this.wakanda.getCatalog()
.then((ds) => {
this.ds = ds;
this.ds.Rpc_reportObjectHelper.isMySystemUp()
.then(statusArr => {
return statusArr;
});
});
}
组件抛出这个关于sys.getStatus()调用的错误:
Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
如果我console.log(sys.getStatus());,它会记录undefined。
我想我只是错过了有关如何正确进行异步调用的一些内容。
【问题讨论】: