这是from the doc:
translate.get('HELLO', {value: 'world'}).subscribe((res: string) => {
console.log(res);
//=> 'hello world'
});
我如何像这样使用数组?
setPayPal(): void {
this.translateService.get(['Client.Pay-With-PayPal', 'Client.Please-Click-And-Make-The-Payment',
]).subscribe(res => {
this.payPal = {
title: res['Client.Pay-With-PayPal'],
payPalUrl: environment.payPalUrl,
description: res['Client.Please-Click-And-Make-The-Payment']
};
});
}
我可以这样获得该值:value: environment.parcelDeliveryCost
gr.json
"Please-Click-And-Make-The-Payment":"Bitte anklicken und die Bezahlung von {{value}} vornehmen",
所以我的问题是如何将其与数组一起应用?
您可以根据可观察对象的性质和要求,使用RxJS forkJoin
,combineLatest
或zip
方法触发多个可观察对象。尝试以下操作
forkJoin({
'Client.Pay-With-PayPal': this.translateService.get('Client.Pay-With-PayPal'),
'Client.Please-Click-And-Make-The-Payment': this.translateService.get('Client.Please-Click-And-Make-The-Payment')
}).subscribe(
response => {
this.payPal = {
title: response['Client.Pay-With-PayPal'],
payPalUrl: environment.payPalUrl,
description: response['Client.Please-Click-And-Make-The-Payment']
};
},
error => {
// handle error
}
);
请注意,forkJoin
仅在所有可观察物完成时发射。如果它们是持久的可观测对象,并且您只想使用第一个发出的值,则可以使用combineLatest
方法并在take(1)
运算符中使用管道。
另外一件事,虽然forkJoin
有一个重载以接受普通对象作为参数(如上所示),但combineLatest
和zip
没有它们。参数应为数组。