javascript - 在角度 8 中为 FormControl 创建扩展方法
问题描述
我想为 formGroup 创建一个扩展,以将波斯日期转换为戈尔吉日期。
这是我的扩展代码:
import { FormControl } from '@angular/forms'
export { }
declare global {
interface FormControl {
ToMiladidate(date: Date): string;
}
}
FormControl.prototype.ToMiladidate = function (date: Date): string {
if (!date)
return this;
return new Date(date).toISOString();
}
但在这一行中,它向我显示错误:
FormControl.prototype.ToMiladidate
告诉我这个错误:
“FormControl”类型上不存在属性“ToMiladidate”
有什么问题 ?我怎么解决这个问题 ???
解决方案
如果这个问题仍然相关,这里是答案:
declare module '@angular/forms' {
interface AbstractControl {
ToMiladidate(date: Date): string;
}
}
AbstractControl.prototype.ToMiladidate = function (date: Date): string {
if (!date)
return this;
return new Date(date).toISOString();
}
解决方案是使用 AbstractFormControl 而不是 FormControl 并声明不是全局模块而是@angular/forms。如果您需要在导入顶部将原型导入app.module.ts时在单独的文件中声明原型:
import './form.prototype';
推荐阅读
- c - 如何在一个函数中声明一个数组,传递它的地址并在另一个函数中动态分配它(在 C 中)?
- protractor - VS 代码 + BitBucket + Bamboo
- node.js - 为什么 app.use() 中的中间件每次都会执行而不考虑 url?
- python - 从列表中检索一系列值
- python - 使用 kafka-python 检索主题中的消息
- r - Hugo Academic 中的出版物编号列表
- javascript - 如何在 jsx 中返回变量并反应到反应组件中
- ruby-on-rails - 如何在 Rails 中使用 password_digest 和 form 更新用户密码
- python - Python 3.7.6 中的列表 + 字符串不一致行为
- html - 将 TFL json 解析为 HTML