我已经创建了一些其他表单组通用的基本模型。
export class BaseResource {
isActive: FormControl;
number: FormControl;
name: FormControl;
type: FormControl;
constructor(
{
number = null,
name = null,
type = null,
isActive = true
}: BaseResourceInterface
) {
this.number = new FormControl(number);
this.name = new FormControl(name);
this.type = new FormControl(type);
this.isActive = new FormControl(isActive);
}
}
this.formBuilder.group(new LocationResource())
.
export class LocationResource extends BaseResource {
cabinetType: FormControl;
serviceLevel: FormControl;
sanitaryStandard: FormControl;
patientsAmount: FormControl;
bedAmount: FormControl;
workPlaceType: FormControl;
building: FormControl;
floor: FormControl;
totalArea: FormControl;
effectiveArea: FormControl;
contacts: FormControl;
constructor(
{
cabinetType,
serviceLevel,
sanitaryStandard,
patientsAmount,
bedAmount,
workPlaceType,
building,
floor,
totalArea,
effectiveArea,
contacts,
...baseProps
}: LocationResourceInterface = {
cabinetType: null,
serviceLevel: null,
sanitaryStandard: null,
patientsAmount: null,
bedAmount: null,
workPlaceType: null,
building: null,
floor: null,
totalArea: null,
effectiveArea: null,
contacts: null,
name: null,
number: null,
type: null,
isActive: null
}
) {
super(baseProps);
this.cabinetType = new FormControl(cabinetType);
this.serviceLevel = new FormControl(serviceLevel);
this.sanitaryStandard = new FormControl(sanitaryStandard);
this.patientsAmount = new FormControl(patientsAmount);
this.bedAmount = new FormControl(bedAmount);
this.workPlaceType = new FormControl(workPlaceType);
this.building = new FormControl(building);
this.floor = new FormControl(floor);
this.totalArea = new FormControl(totalArea);
this.effectiveArea = new FormControl(effectiveArea);
this.contacts = new FormControl(contacts);
}
}
我想有更优雅的方法来定义初始值,这就是我想要的。
BaseResource
(因为它们在类中有初始值)但是TypeScript正在抛出错误,尽管它可以工作。