【问题标题】:i have a problem with reactive forms. it says "addForm" has no initializer我对反应形式有疑问。它说 \"addForm\" 没有初始化程序
【发布时间】:2022-11-10 01:58:07
【问题描述】:

我用一些 formControls 创建了 formGroup。我有一个错误,因为我还没有初始化。我该如何解决?

export class AddFormComponent implements OnInit {
  addForm: FormGroup;  (here is my problem. when i try to add "?" i have an error in html saying addForm<any> | undefined is not assignable to type 'FormGroup<any>)

  onSubmit(){
    console.log(this.addForm);
}

  ngOnInit(): void {
    this.addForm = new FormGroup({
      productName: new FormControl(),
      .....
    });

  }

【问题讨论】:

  • 试试 addForm! :表格组;
  • 我解决了。只是将所有内容从 ngOnInit 移动到构造函数。谢谢
  • 好的。这将解决这个警告,但从长远来看可能不是正确的方法。
  • 它与新特性 Angular 14 Typed Forms 有关

标签: angular typescript forms angular-reactive-forms formgroups


【解决方案1】:

我通过服务做到了,并且完全按照我的意愿工作

第一个组件

ngOnDestroy() {
    this.dataService.songData = this.song; 
 }

第二部分

 ngOnInit() {
    this.song= this.dataService.songData ; 
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多