【问题标题】:Why am I getting such an error? error TS2564:为什么我会收到这样的错误?错误 TS2564:
【发布时间】:2021-04-14 11:36:30
【问题描述】:

从 '@angular/forms' 导入 { FormGroup, FormControl, Validators };

create-post.component.ts

createPostForm: FormGroup;

create-post.component.html

<form [formGroup]="createPostForm" (ngSubmit)="createPost()">

我在 createPostForm: FormGroup 块中收到错误,我在 create-post.component.ts 中创建。

错误 TS2564:属性 'createPostForm' 没有初始化程序,并且未在 t 中明确分配 他是构造函数。

【问题讨论】:

  • 您是否在某处将createPostForm 设置为实际值?大多数时候,仅仅将其声明为一种类型是不够的。
  • 错误信息很清楚,不是吗?您已声明但未初始化您的属性createPostForm。无论是在声明中还是在构造函数中。为createPostForm赋值

标签: angular typescript


【解决方案1】:

你应该像这样初始化 createpost 表单:

createPostForm: FormGroup = new FormGroup({
first: new FormControl('first name'),
last: new FormControl('last name')
});

createPostForm: FormGroup;

constructor() {
    createPostForm=new FormGroup({
        first: new FormControl('first name'),
        last: new FormControl('last name')
   });
}

更多信息:

https://angular.io/api/forms/FormGroup#reset-usage-notes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多