Angular - 如何在formArray.controls中使用'as FormArray':AbstractControl[]



我有一个函数,它返回FormArray的控件:

getFormFControls() {
return this.form.get('opts') ? (<FormArray>this.formQuestions.get('opts')).controls : null;
}

但是当运行Lint检查时,我得到了这个错误:

Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.

我尝试了很多方法来解决这个问题,但当它是FormArray.controls:AbstractControl[]时,我找不到解决方案。

有人能帮我吗?

尝试删除并在末尾添加"as FormArray",但出现错误。

我需要解决这个问题,而不需要在行中添加忽略lint的注释

您的错误试图告诉您更改以下内容:

(<FormArray>this.formQuestions.get('opts')).controls : null;

到此:

(this.formQuestions.get('opts') as FormArray).controls : null;

它们是等效的,但第一个已经不再使用,因为它与JSX混淆了。

最新更新