【发布时间】:2020-03-20 09:59:45
【问题描述】:
我需要将enum type 传递给服务器。但是当我将表单发送到服务器时,它会通过类型 string 发送 enum fild 。
这是我的枚举:
export enum PayTypes {
Free,
Subscribe
}
这是我的表格:
createForm(): void {
this.lessonAddForm = new FormGroup({
payType: new FormControl('', Validators.required),
});
}
addLesson(): void {
const payString = PayTypes[this.lessonAddForm.controls['payType'].value];
const payType: PayTypes = PayTypes[payString];
this.lessonAdd = this.lessonAddForm.value;
this.lessonAdd.payType = payType;
this.subscriptions = this.lessonService
.add(this.lessonAdd)
.subscribe(
res => {
if (res.success === true) {
this.alertService.success('', 'GENERAL.ADD_SUCCESS');
this.backToMenu();
}
}
);
}
我尝试通过这种方式将字符串转换为枚举:
const payString = PayTypes[this.lessonAddForm.controls['payType'].value];
const payType: PayTypes = PayTypes[payString];
this.lessonAdd = this.lessonAddForm.value;
this.lessonAdd.payType = payType;
但它不起作用。
我该如何解决这个问题???
【问题讨论】:
标签: javascript angular typescript ecmascript-6 rxjs