我需要传递enum type
给服务器。但是当我将表单发送到服务器时,它enum fild
按类型发送string
。
这是我的枚举:
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;
但它不起作用。
我怎么解决这个问题????
您必须将方括号内的值转换为这样的字符串
const payType: PayTypes = PayTypes[payString.toString()];
您也可以查看此帖子
更新:检查如何获取payString数据是否与您的枚举定义匹配?请注意,您的payString数据必须与枚举键相同
例如:
PayTypes['Free'] // This will work
和
PayTypes['free'] // This won't
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句