在我的angular2应用程序中,我想按顺序执行可观察订阅,但由于第一次订阅有一个我认为需要大量时间的方法而出现错误。我为不同的组件提供服务,以便他们缓存我的数据并可供所有组件使用。我是Http Observables新手,不知道它们执行的顺序。如何推迟可观察订阅,从第一个可观察到的完成方法执行
PriceList.service.ts
export class PriceListService
{
private priceLists : PriceList[];
private observable : Observable<any>;
public getAll()
{
if(this.priceLists)
{
return Observable.of(this.priceLists);
}
else if (this.observable)
{
return this.observable;
}
else
{
this.observable = this.authHttp.get(this.appService.getApiUrl() + "api/price-list/list")
.map(response => {
this.observable = null;
if(response.status == 400)
{
return "Failure";
}
else if(response.status == 200)
{
let myJson = response.json();
this.priceLists = myJson.data.priceList;
return this.priceLists;
}
})
.share();
return this.observable;
}
}
}
Product.service.ts
export class ProductService
{
products : Product[];
observable : Observable<any>;
priceLists : any;
priceListMap : Map<number, any> = new Map<number, any>();
defaultPriceList : Map<number, any> = new Map<number, any>();
productMap : Map<number , any> = new Map<number , any>();
defaultPriceListId : number;
public getActiveProducts()
{
if(this.products)
{
return Observable.of(this.products);
}
else if (this.observable)
{
return this.observable;
}
else
{
this.observable = this.authHttp.get(this.appService.getApiUrl() + "api/product/list/active")
.map(response => {
this.observable = null;
if(response.status == 400)
{
return "Failure";
}
else if(response.status == 200)
{
let myJson = response.json();
this.products = myJson.data.products;
this.getPriceLists();
return this.products;
}
})
.share();
return this.observable;
}
}
getPriceLists()
{
this.priceListService.getAll().subscribe(
success => { this.priceLists = success; },
error => {},
()=> { this.populatePriceListMap(); }
);
}
populatePriceListMap()
{
for(let priceList of this.priceLists)
{
for(let product of this.products)
{
for(let prices of product.prices)
{
if(priceList.id == prices.priceList.id)
{
this.productMap.set(product.id , {price : prices.price , discount : prices.discount});
}
}
}
if(priceList.isDefault == 1)
{
this.defaultPriceList.set(priceList.id , this.productMap);
this.defaultPriceListId = priceList.id;
}
this.priceListMap.set(priceList.id , this.productMap);
this.productMap = new Map<number , any>();
}
}
getDefaultList()
{
if(this.defaultPriceList){
return Observable.of(this.defaultPriceList.get(this.defaultPriceListId));
}
}
}
ListProduct.component.ts
export class ListProductsComponent implements OnInit
{
products:any = null;
pricesMap : Map<number, any> = new Map<number, any>();
prices:any = [];
discounts:any = [];
ngOnInit()
{
this.productService.getActiveProducts().subscribe(
success =>{
this.products = success;
},
error =>{},
() =>{
this.populatePrices();
}
);
}
populatePrices()
{
this.productService.getDefaultList().subscribe(
success =>
{
this.pricesMap = success;
},
error =>{},
()=>{
for (let product of this.products)
{
let myObject = this.pricesMap.get(product.id);
this.prices[product.id] = myObject.price;
this.discounts[product.id] = myObject.discount;
}
}
);
}
}
错误
它应该做的
我希望我的组件顺序执行的功能。
我想执行
getActiveProducts()
让我productService
获取来自API,并将其存储产品。我想执行
getPriceLists()
的productService
从其他服务获取数据并将其存储在productService
中。我想执行
populatePriceListMap()
,以便它填充服务的不同属性。我想在我的组件中执行
populatePrices()
并将值分配给不同的属性。
问题我现在面临
我想它,而第1步是做其工作执行步骤1和4。我想要显示在步骤4中设置的属性,但它不分配它们并给出错误。