從 Angular 10 升級到 11 并遇到我們在 DecimalPipe 上的擴展的問題。
管道代碼:
export class CustomNumberFormatPipe extends DecimalPipe implements PipeTransform {
transform(value: string): string | null {
if (!value) {
return null;
}
value = value.toString().replace(/,/g, '');
return super.transform(value);
}
和錯誤:
Property 'transform' in type 'CustomNumberFormatPipe' is not assignable to the same property in base type 'DecimalPipe'.
Type '(value: string) => string' is not assignable to type '{ (value: string | number,
digitsInfo?: string, locale?: string): string; (value: null, digitsInfo?: string, locale?:
string): null; (value: string | number, digitsInfo?: string, locale?: string):
string; }'.
Type 'string' is not assignable to type 'null'.
和 DecimalType 定義:
export declare class DecimalPipe implements PipeTransform {
private _locale;
constructor(_locale: string);
transform(value: number | string, digitsInfo?: string, locale?: string): string | null;
transform(value: null | undefined, digitsInfo?: string, locale?: string): null;
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null;
static ?fac: ?ngcc0.??FactoryDef<DecimalPipe, never>;
static ?pipe: ?ngcc0.??PipeDefWithMeta<DecimalPipe, "number">;
}
到目前為止,我找到的唯一解決方案是共同修改實際的 DecimalPipe,但我真的沒有信心這樣做。提前致謝。
uj5u.com熱心網友回復:
只需轉到DecimalPipe宣告并將其transform簽名復制到您的管道中:
transform(value: number | string, digitsInfo?: string, locale?: string): string | null;
transform(value: null | undefined, digitsInfo?: string, locale?: string): null;
transform(value: number | string | null | undefined, digitsInfo?: string, locale?: string): string | null {
if (!value) {
return null;
}
value = value.toString().replace(/,/g, '');
return super.transform(value);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/332656.html
上一篇:是否可以使用Vue2在組件的資料部分中使用ref訪問HTML元素?
下一篇:沒有價值的角形貨幣管
