Angular使用ng build打包报错 Property 'setControl' does not exist on type 'AbstractControl'.解决方法

在项目打包出现setControl报错解决方法:

对于用setControl设置多层级表单的值的用法,会出现这个报错:

以下是正确的写法:

get services(): FormArray {
    return this.sltForm.get("Service") as FormArray;
  }

this.services.setControl("SvcInetUrl", this.fb.array(sltService.SvcInetUrl.map(
            url =>
              this.fb.group({
                url: [url.url, Validators.required],
                urlType: [url.urlType, Validators.required]
              })
          )));

如果直接设置SvcInetUrl就会出现报错:(示例报错代码)

this.sltForm.setControl("Service", this.fb.group({
           SvcInetUrl: this.fb.array(
            sltService.svcInetUrls.map(url =>
               this.fb.group({
                 url: [url.url, Validators.required],
                  urlType: [url.urlType, Validators.required]
               })
             )
            )

猜你喜欢

转载自blog.csdn.net/qq_29483485/article/details/84652523
今日推荐