在angular 5中执行双重ajax调用

时间:2018-07-17 22:22:35

标签: angular typescript angular5

我正在尝试发出双重Http请求,一个在我收到第一个结果之后 但我似乎错过了一些东西:

getParkingSpots(date) {
 var gmt = this.getFuseauHoraire().subscribe(data=>{
   if(data=="GMT"){
     return this.http.get(url1+date+"GMT").map(res=>res);//this is the data i want never mind the urls
   }
   else{
      return this.http.get(url2+date).map(res=>res);//this is the data i want
   }
 })

}

this.parkpace.getParkingSpots(dd).subscribe(data=> {//here is where i use it
  this.ParkingSpaces = data; 
  console.log(data);
  console.log("dd="+dd);
});

3 个答案:

答案 0 :(得分:1)

ol {
  list-style-type: none;
  counter-reset: item;
  margin: 0;
  padding: 0;
}

ol > li {
  display: table;
  counter-increment: item;
  margin-bottom: 0.6em;
}

ol > li:before {
  content: counters(item, ".") ". ";
  display: table-cell;
  padding-right: 0.6em;    
}

li ol > li {
  margin: 0;
}

li ol > li:before {
  content: counters(item, ".") " ";
} 

答案 1 :(得分:0)

使用switchMap从一个Observable开始,并在保持流活跃的同时切换到另一个:

getParkingSpots(date) {
 return this.getFuseauHoraire().pipe(switchMap(data=>{
   if(data=="GMT"){
     return this.http.get(url1+date+"GMT").pipe(map(res=>res));//this is the data i want never mind the urls
   }
   else{
      return this.http.get(url2+date).pipe(map(res=>res));//this is the data i want
   }
 }))

答案 2 :(得分:0)

webpack_require是生成的包的一部分。您可能正在使用webpack进行操作,或者未正确导入模块。

就您的代码示例而言,没有任何问题可以引发此错误。