2017-09-04 78 views
1

我工作的一个角CLI项目创建一个数组,我有一个评估多HTTPS请求.`使用多个HTTPS请求打字稿

this.files = this.http.get("assets/L10n/customer.json") 
 
         .map(res => res.json()) 
 
         .subscribe(data => console.log(data)); 
 
    } 
 
    this.files2 = this.http.get("assets/L10n/customer2.json") 
 
         .map(res => res.json()) 
 
         .subscribe(data => console.log(data)); 
 
    }

我想回到这个方法.files和this.files2作为数组。任何帮助appreciated`

回答

0

您可以使用forkJoin

let first = this.http.get("assets/L10n/customer.json") 
    .map(res => res.json()); 
let second = this.http.get("assets/L10n/customer2.json") 
    .map(res => res.json()); 

Observable.forkJoin(first, second).subscribe(
    (resultArray) => { 
     // `resultArray` is a combined array of first and second results 
     console.log(resultArray); 
    } 
) 
+0

感谢它的工作.console即时通讯获取价值。但当我尝试在另一种方法中使用该值时,它给予未定义 – dockerrrr

+0

,而不是控制台我添加this.result = data.And当我尝试打印this.result在另一个方法的说法undefined – dockerrrr

+0

你在访问'this.data'吗?请记住这些操作是异步的。只有在HTTP调用完成后才能获得该值 - 这是异步的。 – Saravana