我正在尝试实例化一个DatePipe
我的 Angular2 应用程序中要使用的对象transform(...)
我正在开发的组件中的功能。
// ...
import { DatePipe } from '@angular/common';
@Component({...})
export class PanelComponent implements OnInit {
// ...
datePipe: DatePipe = new DatePipe(); // Error thrown here
// ...
}
该代码段在 RC5 中运行良好。现在我尝试升级到 Angular2 最终版本并在运行时收到此错误ng serve
or ng build
,
~/tmp/broccoli_type_script_compiler-input_base_path-XitPWaey.tmp/0/src/app/panel/panel.component.ts (33, 24):
Supplied parameters do not match any signature of call target.
我该如何解决这个问题?还有另一种实例化 Pipe 的方法吗?或者 Angular 是否已停止支持组件内部 Pipes 的实例化?
如果您查看源代码,您将看到 DatePipe 构造函数要求提供必需的参数:
constructor(@Inject(LOCALE_ID) private _locale: string) {}
DataPipe 没有默认区域设置
https://github.com/角度/角度/blob/2.0.0/modules/%40 角度/common/src/pipes/datepipe.ts#L97 https://github.com/angular/angular/blob/2.0.0/modules/%40angular/common/src/pipes/date_pipe.ts#L97
这就是打字稿给出错误的原因。
这样你就必须初始化你的变量,如下所示:
datePipeEn: DatePipe = new DatePipe('en-US')
datePipeFr: DatePipe = new DatePipe('fr-FR')
constructor() {
console.log(this.datePipeEn.transform(new Date(), 'dd MMMM')); // 21 September
console.log(this.datePipeFr.transform(new Date(), 'dd MMMM')); // 21 septembre
}
希望对您有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)