简体   繁体   English

如何在角度5的纯管道中使用HTTP调用

[英]How to use HTTP calls in pure pipe in angular 5

I am creating a pipe to convert one currency value to another. 我正在创建将一种货币值转换为另一种货币值的管道。 I am making HTTP call to convert value. 我正在进行HTTP调用以转换值。

@Pipe({
    name: "currencyConverter"
})
export class CurrencyConverterPipe implements PipeTransform {

    transform(value: number, currencyPair: string): number {
        let sourceCurrency = currencyPair.split(":")[0];
        let destinationCurrency = currencyPair.split(":")[1];
        this.currencyConverterService.convert(sourceCurrency, destinationCurrency).subscribe(successData => {
            return successData['fiatValue'] * value;
        }, errorData => {
            console.error(errorData);
        })
    }
}

In HTML 在HTML中

<p> {{ value | currencyConverter:'BTC:USD'}}</p>

I am unable to see any value in UI. 我在UI中看不到任何值。 When i make my pipe impure , Then it works but it is making many HTTP calls to server. 当我使管道impure ,它可以工作,但是它对服务器进行了许多HTTP调用。 Is there any way to make HTTP call without using pure: false in pipe 有没有办法在不使用pure: false情况下进行HTTP调用pure: false管道中为pure: false

This pipe isn't pure by design. 该管道不是纯设计的。

In order to not cause multiple requests, it should return same observable that won't do additional requests until input is changed, something like: 为了不引起多个请求,它应该返回相同的observable,除非更改输入,否则不会再执行其他请求,例如:

@Pipe({
  name: "currencyConverter",
  pure: false
})
export class CurrencyConverterPipe  {
  private sourceCurrency;
  private destinationCurrency;
  private valueSubject = new Subject();
  private value$ = this.valueSubject.asObservable().distinctUntilChanged()
  .switchMap(value => {
     return this.currencyConverter
     .convert(sourceCurrency, destinationCurrency))
     .map((data) => data * value);
  });

  constructor(private currencyConverter: ...) {}

  transform(value: number, currencyPair: string): number {
    this.sourceCurrency = currencyPair.split(":")[0];
    this.destinationCurrency = currencyPair.split(":")[1];
    this.valueSubject.next(value);
    return this.value$;
  }
}

Since it is supposed to be used only in conjunction with async pipe, it makes sense to join them together and extend AsyncPipe : 由于应该仅将其与async管道结合使用,因此将它们结合在一起并扩展AsyncPipe才有意义:

@Pipe({
  name: "currencyConverterAsync",
  pure: false
})
export class CurrencyConverterPipe extends AsyncPipe {
  private sourceCurrency;
  private destinationCurrency;
  private valueSubject = new Subject();
  private value$ = this.valueSubject.asObservable().distinctUntilChanged()
  .switchMap(value => {
     return this.currencyConverter
     .convert(sourceCurrency, destinationCurrency))
     .map((data) => data * value);
  });

  constructor(cdRef: ChangeDetectorRef, private currencyConverter: ...) {
    super(cdRef);
  }

  transform(value: number, currencyPair: string): number {
    this.sourceCurrency = currencyPair.split(":")[0];
    this.destinationCurrency = currencyPair.split(":")[1];
    this.valueSubject.next(value);

    return super.transform(this.value$);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM