Angular 在component.ts中使用管道
我有一个管道来转换货币,如下所示Angular 在component.ts中使用管道,angular,typescript,pipe,Angular,Typescript,Pipe,我有一个管道来转换货币,如下所示 import { Pipe, PipeTransform } from '@angular/core'; import { LocalStorageService } from 'storage.service'; import { CurrencyUpdate } from 'update'; import { Observable } from 'rxjs/Observable'; @Pipe({name: 'currConvert'}) export c
import { Pipe, PipeTransform } from '@angular/core';
import { LocalStorageService } from 'storage.service';
import { CurrencyUpdate } from 'update';
import { Observable } from 'rxjs/Observable';
@Pipe({name: 'currConvert'})
export class CurrConvertPipe implements PipeTransform {
errorMessage: string;
constructor(private currencyStorage: LocalStorageService, private currencyConversion: CurrencyUpdate) {
}
transform(value: number, toCurrency: string, baseCurrency: string): any {
if (toCurrency && baseCurrency) {
const outputRate = this.currencyStorage.getCurrencyRate(baseCurrency, toCurrency);
if (!outputRate) {
return this.currencyConversion.getExchangeRate(toCurrency, baseCurrency).map((rate: any) => {
const currency = {
fromCurrency: baseCurrency,
toCurrency,
exchangeRate: rate
};
this.currencyStorage.saveCurrencies(currency);
return value * rate;
}, (error: any) => this.errorMessage = error);
} else {
return Observable.of(value * outputRate);
}
}
};
}
this.convertedPrice = CurrConvertPipe.prototype.transform(this.Price, this.selectedCurrency, this.movieticket.price.currency);
在component.html中使用时效果非常好。在一种情况下,我需要在组件.ts中使用它,因此我按如下方式使用它
import { Pipe, PipeTransform } from '@angular/core';
import { LocalStorageService } from 'storage.service';
import { CurrencyUpdate } from 'update';
import { Observable } from 'rxjs/Observable';
@Pipe({name: 'currConvert'})
export class CurrConvertPipe implements PipeTransform {
errorMessage: string;
constructor(private currencyStorage: LocalStorageService, private currencyConversion: CurrencyUpdate) {
}
transform(value: number, toCurrency: string, baseCurrency: string): any {
if (toCurrency && baseCurrency) {
const outputRate = this.currencyStorage.getCurrencyRate(baseCurrency, toCurrency);
if (!outputRate) {
return this.currencyConversion.getExchangeRate(toCurrency, baseCurrency).map((rate: any) => {
const currency = {
fromCurrency: baseCurrency,
toCurrency,
exchangeRate: rate
};
this.currencyStorage.saveCurrencies(currency);
return value * rate;
}, (error: any) => this.errorMessage = error);
} else {
return Observable.of(value * outputRate);
}
}
};
}
this.convertedPrice = CurrConvertPipe.prototype.transform(this.Price, this.selectedCurrency, this.movieticket.price.currency);
这抛出了一个错误,即
错误类型错误:无法读取未定义的属性“getCurrencyRate”
我尝试创建管道实例,但它不允许我这样做。如何在TS内部消费?您不能将转换方法用作静态。。。因为管道构造函数中有依赖项,这些依赖项可以从您的角度解析,您需要使用。您需要通过依赖项注入在组件中导入CurrConvertPipe的实例,就像在依赖项CurrencyUpdate和LocalStorageService的管道中一样。诸如此类:
@Component
class SomeComponent {
constructor(private currConvertPipe: CurrConvertPipe ) { }
method() {
this.convertedPrice = this.currConvertPipe.transform(this.Price, this.selectedCurrency, this.movieticket.price.currency);
}
}
希望这有帮助。您不能将转换方法用作静态。。。因为管道构造函数中有依赖项,这些依赖项可以从您的角度解析,您需要使用。您需要通过依赖项注入在组件中导入CurrConvertPipe的实例,就像在依赖项CurrencyUpdate和LocalStorageService的管道中一样。诸如此类:
@Component
class SomeComponent {
constructor(private currConvertPipe: CurrConvertPipe ) { }
method() {
this.convertedPrice = this.currConvertPipe.transform(this.Price, this.selectedCurrency, this.movieticket.price.currency);
}
}
希望这有帮助。酷!我应该查一下!无论如何,我在一个共享模块中使用了这个管道,但是组件仍然抱怨我应该在提供者下添加它。有什么想法吗?@Sajeetharan,因为管道不是DI系统的一部分!我应该查一下!无论如何,我在一个共享模块中使用了这个管道,但是组件仍然抱怨我应该在提供者下添加它。有什么想法吗?@Sajeetharan,因为管道不是DI系统的一部分