javascript - 当我在我的 Angular 应用程序中进行验证时,我面临在“AbstractControl[] 错误类型上找不到带有“字符串”类型参数的索引签名
问题描述
当我在我的角度应用程序中进行验证时,我遇到了这个错误:
src/app/register/register.component.ts:45:39 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'AbstractControl[] | { [key: string]: AbstractControl; }'.
No index signature with a parameter of type 'string' was found on type 'AbstractControl[] | { [key: string]: AbstractControl; }'.
45 return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这是错误来自的方法:
matchValues(matchTo: string): ValidatorFn {
return (control: AbstractControl) => {
return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
}
}
我刚刚开始使用 angular 和 TypeScript,但我不知道如何解决它。任何帮助,将不胜感激。
解决方案
您必须定义对象具有哪种索引类型。在您的情况下,它是基于字符串的索引。
matchValues(matchTo: string): ValidatorFn {
return (control: AbstractControl) => {
return control.value === (control.parent.controls as { [key: string]: AbstractControl })[matchTo].value ? null : { isMatching: true };
}
}
推荐阅读
- android - 在 Android Studion 中为两个 URL 使用 AsyncTask 时出现问题
- r - ggplot2:geom_bar 和 position_dodge:不以 x 轴为中心(因子)
- python-3.x - Python firebase_admin.credentials 没有属性 Certificate
- ruby-on-rails - 如何在不出现“我们很抱歉,但出了点问题”的情况下推送到 Heroku
- r - 如何根据列值在行上循环操作?R
- c - 如何在 C 中以适当的间距生成随机数?
- azure - 权限不足,无法完成操作 - 服务主体
- android-seekbar - 从 seekBar android 中删除数字
- reactjs - react-chartjs-2 在调整窗口大小之前不显示行
- r - 如何在闪亮的应用程序中设置下载按钮的样式