【发布时间】:2017-04-23 23:54:39
【问题描述】:
在我的 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()ofproductService以从其他服务获取数据并将其存储在productService中。我想执行
populatePriceListMap(),以便它填充不同的服务属性。我想在我的组件中执行
populatePrices(),并为不同的属性赋值。
我面临的问题
我认为它在第 1 步完成工作时执行第 1 步和第 4 步。我想显示在步骤 4 中设置的属性,但它没有分配它们并给出错误。
【问题讨论】:
标签: angular typescript rxjs observable