【发布时间】:2019-03-10 07:20:00
【问题描述】:
加载第一个 API 时,我需要获取 value 并将其传递给第二个 API 以获取详细信息。随着我当前的代码变量变得未定义。这是我的代码:
app.ts
export class DailyEndorsementComponent implements OnInit {
public selectedShop: string;
shop: string[];
shopData: ShopData;
constructor(private endorsementService: EndorsementService) {}
ngOnInit() {
this.getDates();
this.loadShopList();
}
setPayload() {
return {
'startDate': this.fromDate,
'endDate': this.toDate,
'settlementBank': this.selectedShop,
}
}
getDates(range: any) {
this.fromDate = this.datePipe.transform(range.fromDate, 'yyyy-MM-dd');
this.toDate = this.datePipe.transform(range.toDate, 'yyyy-MM-dd');
this.loadMerchantDailyEndorsement();
}
loadShopList() {
this.endorsementService.shopList()
.subscribe(response => {
this.shop = response;
this.selectedShop = this.banks[0];
})
}
loadMerchantDailyEndorsement() {
this.endorsementService.getMerchantEndorsement(this.setPayload())
.subscribe((response: EndorsementResponseInterface) => {
this.shopData = response;
}
}
app.service.ts
export class EndorsementService extends RestService {
private BASE_URL_MERCHANT: string = '/settlement/merchant/settlement';
private BASE_URL_CUSTOMER: string = '/settlement/customer/settlement';
private headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': localStorage.getItem('token')
});
constructor(protected httpClient: HttpClient,
protected injector: Injector,
protected snackBar: MatSnackBar) {
super(injector);
}
getMerchantEndorsement(body: any): Observable < EndorsementResponseInterface > {
return this.post < EndorsementResponseInterface >
(this.getFullUrl(`${this.BASE_URL_MERCHANT}/daily/get`), body, {
headers: this.headers
});
}
shopList(): Observable < string[] > {
return this.get < string[] >
(this.getFullUrl(`/settlement/settlementbanks`), {
headers: this.headers
});
}
}
我需要从 loadShopList() 中获取价值,然后调用并将其传递给 loadMerchantDailyEndorsement()。如果我错过了什么,请告诉我。
【问题讨论】:
-
请简化您的问题,您可以获得更好的答案
标签: angular typescript api asynchronous