如何在接口实例中使用FormControl结果



我在FormGroup实例中有表单,提交后我应该在接口方案创建的对象中设置填充值。但当我直接尝试这样做时,我遇到了错误:

中的错误src/app/modules/objects/object-form/object-formcomponents/object-information/object-information.component.ts(97,9(:错误TS2322:类型"{title:any;Type_id:any;basises:any;";问题:任何;material_id:any;'不是可分配给类型"ObjectFormComponent"。对象文字只能指定已知属性,类型中不存在"title"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)
};

Into表单中所有具有any类型的控件,这对接口无效。

我该怎么做?

如果您通读错误消息,您的错误似乎非常明显。具体地说,'title' does not exist in type 'ObjectFormComponent'。请注意,该错误并没有描述您发布的代码——请查看错误中的文件名和行号,以找到有问题的行。

您有一个对象,它定义了5个属性:titletype_idbasisesproblemsmaterial_id。您使用您的类型告诉TypeScript在某个地方需要ObjectFormComponent类型的对象,但却给了它该对象。问题是,您的对象有一个名为title的字段,它在ObjectFormComponent的定义中不存在。

这有道理吗?

最新更新