如何从不同的服务发出异步请求[Angular]

时间:2019-03-23 16:08:10

标签: angular httprequest

我有3种服务,它们具有类似的方法,可从数据库返回数据。 我在组件中使用它们,并希望将每个数据存储在相应的响应中 组件数组。我如何从其他服务中获取其余数据? 我对forkjoin等并不十分熟悉。如果您举个例子,那会很好。

Map<Integer, String> myTreeMap = new TreeMap<>();
myTreeMap.put(1, "one");
myTreeMap.put(2, "two");
myTreeMap.put(3, "three");
myTreeMap.put(4, "four");
myTreeMap.put(5, "five");
String fourth = myTreeMap.entrySet().stream()
   .skip(3)
   .map(map -> map.getValue()).findFirst().get();
System.out.println("fourth value in map is: " + fourth);

3 个答案:

答案 0 :(得分:0)

您必须订阅其他服务(例如产品服务)并将其包含在提供商中

    constructor(private productService: ProductService,
                private brandService: BrandService,
                private categoryService: CategoryService) {
        this.productService.getJsonProducts().subscribe(product => {
          this.productList = product;
        })
        this.brandService.getJsonBrands().subscribe(brands=> {
           this.brandList = brands;
        })
        this.CategoryService.getJsonCategories().subscribe(categories=> {
           this.categoryList= categories;
        })
    }

答案 1 :(得分:0)

使用Rxjs中的ForkJoin或CombineLastest,两者之间的区别在这里说明:Rxjs: Observable.combineLatest vs Observable.forkJoin

ngOnInit() {
forkJoin(
      this.productService.getJsonProducts(),
      this.brandService.getJsonBrands(),
      this.categoryService.getJsonCategories() 
    )
    .subscribe(([res1, res2, res3]) => {
      this.productList = res1;
      this.brandList = res2;
      this.categoryList = res3;
    });
}

答案 2 :(得分:0)

您可以使用import pandas as pd import io data=""" ;Barcode;Created;Hash;Modified;Tag;Tag2 0;9780735711020;2019-02-22T22:35:06.628Z;None;2019-02-22T22:35:06.628Z;NEED_PICS; 1;3178041328890;2019-02-22T22:37:44.546Z;None;2019-02-22T22:37:44.546Z;DISPLAY; 2;8718951129597;2019-02-23T04:53:17.925Z;None;2019-02-23T04:53:17.925Z;DISPLAY; 3;3770006053078;2019-02-23T05:25:56.454Z;None;2019-02-23T05:25:56.454Z;DISPLAY; 4;3468080404892;2019-02-23T05:26:39.923Z;None;2019-02-23T05:26:39.923Z;NEED_PICS; 5;3517360013757;2019-02-23T05:27:24.910Z;None;2019-02-23T05:27:24.910Z;DISPLAY; 6;3464660000768;2019-02-23T05:27:51.379Z;None;2019-02-23T05:27:51.379Z;DISPLAY; 7;30073357;2019-02-23T06:20:53.075Z;None;2019-02-23T06:20:53.075Z;NEED_PICS; 8;02992;2019-02-23T06:22:57.326Z;None;2019-02-23T06:22:57.326Z;NEED_PICS; 9;3605532558776;2019-02-23T06:23:45.010Z;None;2019-02-23T06:23:45.010Z;NEED_PICS; 10;3605532558776;2019-02-23T06:23:48.291Z;None;2019-02-23T06:23:48.291Z;NEED_PICS; 11;3605532558776;2019-02-23T06:23:52.579Z;None;2019-02-23T06:23:52.579Z;NEED_PICS; """ from io import StringIO TESTDATA = StringIO(data) df = pd.read_csv(TESTDATA, sep=";") df["Created"] = pd.to_datetime(df["Created"],errors='coerce') df["Barcode"] = df["Barcode"].astype(str) df.set_index(df.columns[0], inplace=True) df2 = df #df[df.Hash != "None"] df3 = df2 df3 = df3.loc[df3.Tag == "DISPLAY"] df = df2.merge(df3, on='Created', how='outer').fillna(0) df['sum'] = df['Barcode_x']+df['Barcode_y'] df.plot(df['sum'], df['Created']) combineLatest。用法非常相似。 但是您不应该像forkJoin那样退订combineLatest

forkJoin

forkJoin -完成所有可观察对象后,从每个观察对象中发出最后一个发出的值。

combineLatest -当任何可观察对象发出一个值时,请从每个观察对象中发出最新的值。