如何将 FormControl 结果用于接口实例?
How to use FormControl result into interface instance?
我在 FormGroup
实例中有表单,提交后我应该将填充值设置到接口方案创建的对象中。但是当我直接尝试这样做时,我遇到了错误:
ERROR in
src/app/modules/objects/object-form/object-form-components/object-information/object-information.component.ts(97,9):
error TS2322: Type '{ title: any; type_id: any; basises: any;
problems: any; material_id: any; ' is not
assignable to type 'ObjectFormComponent'. Object literal may only
specify known properties, and 'title' does not exist in type
'ObjectFormComponent'.
const controls = this.informationForm.controls;
export interface ObjectCreateRequest {
title: string;
type_id: string;
problems: string[];
material_id: string;
}
const request: ObjectCreateRequest = {
title: controls.title.value,
type_id: controls.type.value.id,
problems: controls.problems.value.map(item => item.id),
material_id: (controls.material.value ? controls.material.value.id : null)
};
形成具有 any
类型的所有控件,这对界面无效。
我该怎么做?
如果您通读错误消息,您的错误似乎很清楚。具体来说,'title' does not exist in type 'ObjectFormComponent'
。请注意,该错误并未描述您发布的代码——查看错误中的文件名和行号以找到有问题的行。
您在某处有一个对象定义了 5 个属性:title
、type_id
、basises
、problems
和 material_id
。您使用您的类型告诉 TypeScript 在某处期望类型为 ObjectFormComponent
的对象,但是却给了它那个对象。问题是,您的对象有一个名为 title
的字段,它在 ObjectFormComponent
的定义中不存在。
这有意义吗?
我在 FormGroup
实例中有表单,提交后我应该将填充值设置到接口方案创建的对象中。但是当我直接尝试这样做时,我遇到了错误:
ERROR in src/app/modules/objects/object-form/object-form-components/object-information/object-information.component.ts(97,9): error TS2322: Type '{ title: any; type_id: any; basises: any; problems: any; material_id: any; ' is not assignable to type 'ObjectFormComponent'. Object literal may only specify known properties, and 'title' does not exist in type 'ObjectFormComponent'.
const controls = this.informationForm.controls;
export interface ObjectCreateRequest {
title: string;
type_id: string;
problems: string[];
material_id: string;
}
const request: ObjectCreateRequest = {
title: controls.title.value,
type_id: controls.type.value.id,
problems: controls.problems.value.map(item => item.id),
material_id: (controls.material.value ? controls.material.value.id : null)
};
形成具有 any
类型的所有控件,这对界面无效。
我该怎么做?
如果您通读错误消息,您的错误似乎很清楚。具体来说,'title' does not exist in type 'ObjectFormComponent'
。请注意,该错误并未描述您发布的代码——查看错误中的文件名和行号以找到有问题的行。
您在某处有一个对象定义了 5 个属性:title
、type_id
、basises
、problems
和 material_id
。您使用您的类型告诉 TypeScript 在某处期望类型为 ObjectFormComponent
的对象,但是却给了它那个对象。问题是,您的对象有一个名为 title
的字段,它在 ObjectFormComponent
的定义中不存在。
这有意义吗?