typescript 元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型“(controlName:字符串)=>布尔值'

bsxbgnwa  于 2022-11-18  发布在  TypeScript
关注(0)|答案(4)|浏览(225)
hasError(typeofvalidator:string, controlname:string) : boolean 
{
    return this.CustomerModel.formCustomerGroup.contains[controlname].hasError(typeofvalidator);
 }

我正在学习Angular ,在课程视频有相同的代码,但它在课程中运行,但我得到错误.我不知道我做错了什么需要帮助,请.

zpjtge22

zpjtge221#

也许可以尝试将contains后面的方括号[]替换为普通括号()?看起来错误提示您正在尝试对函数进行索引。
所以看起来应该更像这样:

return this.CustomerModel.formCustomerGroup.contains(controlname).hasError(typeofvalidator);

在此之后,我假设您最初的错误将得到解决,但可能会有另一个错误。

hrysbysz

hrysbysz2#

要调试此问题,请将每个步骤分解为几个强类型变量。

hasError(typeofvalidator:string, controlname:string) : boolean 
{
  const customerModel: {formerCustomerGroup: string} = this.CustomerModel;

  const fCG: string = customerModel.formCustomerGroup;

  return fCG.contains[controlname].hasError(typeofvalidator);
}

我不知道为什么你的方法不起作用,我肯定我的方法也不会起作用,不管你的方法是什么原因不起作用,但它会把问题分解成一个非常容易识别的点,然后你就可以从那里开始工作。
当你修复了它,你可以改变它回到较短的版本,如果你喜欢,但在这里你遇到了一个障碍,它的时间去搜索。
祝你好运!

kyks70gy

kyks70gy3#

它不包含您应该使用控件而不是尝试此操作
返回这个.NewCustomer .FormCustomerGroup .控件[控件名称] .hasError(验证器类型);

4sup72z8

4sup72z84#

问题就在这里:

hasError(typeofValidator: string, controlname: string): boolean {
    return this.customerModel.formCustomerGroup.controls[controlname].hasError(typeofValidator);
}

解决方法:
只需将controlname替换为ControlName即可。

hasError(typeofValidator: string, controlName: string): boolean {
    return this.customerModel.formCustomerGroup.controls[controlName].hasError(typeofValidator);
}

相关问题