将参数与翻译服务和字符串数组一起应用

问题描述 投票:0回答:1

这是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",

所以我的问题是如何将其与数组一起应用?

angular typescript ionic-framework ngx-translate ionic5
1个回答
0
投票

您可以根据可观察对象的性质和要求,使用RxJS forkJoincombineLatestzip方法触发多个可观察对象。尝试以下操作

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有一个重载以接受普通对象作为参数(如上所示),但combineLatestzip没有它们。参数应为数组。

© www.soinside.com 2019 - 2024. All rights reserved.