【问题标题】:Angular2 multiple REST endpoint callsAngular2 多个 REST 端点调用
【发布时间】:2018-02-28 08:19:19
【问题描述】:

我试图在我的 Angular 2 组件中使用多个 REST 端点调用。问题是,我没有从任何人那里得到任何数据。我要做的是:

getData() {
   myService.getDataA().subscribe(data => { this.dataA = data })
   myService.getDataB().subscribe(data => { this.dataB = data })
   myService.getDataC().subscribe(data => { this.dataC = data })

我的服务是:

getDataA() {
   return this.http.get(MY_URL).map(data => data.json());

getDataB() {
   return this.http.get(MY_URLB).map(data => data.json());

getDataC() {
   return this.http.get(MY_URLC).map(data => data.json());

有没有办法等到所有异步任务完成后再显示模板?

谢谢

【问题讨论】:

  • 如果this.dataAthis.dataBthis.dataC不为空且已定义,您可以签入模板

标签: angular rest asynchronous angular2-template angular2-services


【解决方案1】:

【讨论】:

    【解决方案2】:

    做这样的事情

    Observable.forkJoin(getDataA(), getDataB(), getDataC()).subscribe(response => {
    response[0] //contain getDataA response
    response[1] //contain getDataB response
    response[2] //contain getDataC response
    }, e => { //get the errors });
    

    【讨论】:

    • 不幸的是,我尝试了这个,当我尝试使用 console.log 作为输出时,我仍然不确定。我注意到我没有从 subscribe 方法获得 console.log 。但是当我检查开发者控制台时,后端被调用了。
    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 2019-09-02
    • 2017-11-28
    • 2016-04-26
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多