【问题标题】:Angular - Element implicitly has an 'any' type because type 'AbstractControl' has no index signatureAngular - 元素隐式具有“任何”类型,因为类型“AbstractControl”没有索引签名
【发布时间】:2021-08-24 16:44:10
【问题描述】:

在我的 Angular-11 中,我的打字稿中有这段代码:

multistep = new FormGroup({
      userDetails: new FormGroup({
        first_name: new FormControl(''),
        last_name: new FormControl(''),
        other_name: new FormControl(''),
        email: new FormControl(''),
        mobile_number: new FormControl(''),
        gender: new FormControl(''),
        marital_status: new FormControl(''),
        employee_photo: new FormControl('')
      })
});

get userDetails(){
   return this.multistep.controls['userDetails']['controls'];
 }

但是我收到了这个错误:

元素隐含地具有“任何”类型,因为类型“AbstractControl”没有索引签名。您的意思是调用“get”吗?

这是突出显示的:

this.multistep.controls['userDetails']['controls'];

如何解决?

谢谢

【问题讨论】:

  • 错误告诉您如何解决问题。不要使用括号语法,而是使用:this.multistep.get('userDetails') as FormGroup

标签: angular


【解决方案1】:

您需要将 userDetails 转换为 FormGroup userDetails as FormGroup。

  get userDetails(){
    return (this.multistep.controls.userDetails as FormGroup).controls;
  }

【讨论】:

    【解决方案2】:

    您可以改用这个:

    this.multistep.get('userDetails').controls
    

    get 方法是访问 formGroup 中的控件的方法

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 2018-09-06
      • 2019-05-12
      • 1970-01-01
      • 2017-06-30
      • 2019-04-01
      • 2019-01-06
      • 2019-10-03
      • 2021-03-19
      相关资源
      最近更新 更多