我该如何解决这个错误。我在“注册表单:FormGroup;”这一行出现错误[复制]
Posted
技术标签:
【中文标题】我该如何解决这个错误。我在“注册表单:FormGroup;”这一行出现错误[复制]【英文标题】:How do i fix this error. i am getting error on this line " registrationForm : FormGroup;" [duplicate] 【发布时间】:2021-09-15 11:30:47 【问题描述】:***错误:src/app/r-form-example/r-form-example.component.ts:11:3 - 错误 TS2564:属性 'registrationForm' 没有初始化程序,并且不确定 在构造函数中赋值。
11个注册表格:FormGroup;
我遇到了上述错误。
import Component, OnInit from '@angular/core';
import FormBuilder, FormControl, FormGroup from '@angular/forms';
@Component(
selector: 'app-r-form-example',
templateUrl: './r-form-example.component.html',
styleUrls: ['./r-form-example.component.css']
)
export class RFormExampleComponent implements OnInit
registrationForm : FormGroup;
constructor( private fb : FormBuilder)
ngOnInit(): void
// this.registrationForm = new FormGroup(
// 'firstName': new FormControl(),
// 'lastName': new FormControl(),
// )
this.registrationForm = this.fb.group(
'firstName': new FormControl(),
'lastName':new FormControl()
);
addRegistration()
console.log(this.registrationForm.value);
【问题讨论】:
注册表! : 表单组; 这能回答你的问题吗? Property '...' has no initializer and is not definitely assigned in the constructor 试试这个!注册表格!:FormGroup; 【参考方案1】:在你的打字稿设置上——你不能在没有赋值的情况下声明变量(当你声明它或在构造函数中......)。 你可以这样声明:
registrationForm!: FormGroup
或者在构造函数中赋值
【讨论】:
以上是关于我该如何解决这个错误。我在“注册表单:FormGroup;”这一行出现错误[复制]的主要内容,如果未能解决你的问题,请参考以下文章