【发布时间】:2022-01-17 01:13:29
【问题描述】:
我目前正在使用 TS 开发一个 Vue 2 项目,使用这些库进行组件设置:vue-class-component 和 vue-property-decorator
我需要创建一个表单组件,但我很难进行数据绑定,因为当我为表单组件使用 data() 选项时,它会在编译时产生错误。
有没有办法在不使用 data() 选项的情况下绑定输入数据?
我得到的错误:
ERROR in /path/to/components/folder/Form.vue
Property 'id' does not exist in type 'Form'.
有关更多上下文,这是我的代码:
@Component
export default class Form extends Vue {
//Bind form data
data() {
return {
id: "",
name: "",
age: 0,
amountA: 0,
amountB: 0,
capitalA: 0,
capitalB: 0
}
}
onSubmit(e: any): void {
e.preventDefault();
//Create new class object
const newClass = {
id: this.id,
name: this.name,
age: this.age,
amountA: this.amountA,
amountB: this.amountB,
capitalA: this.capitalA,
capitalB: this.capitalB
}
//Emit the class object to parent component
this.$emit("add-class", newClass);
this.$emit("update-total");
//Reset Form
this.id = ""
this.name = ""
this.age = 0
this.amountA = 0
this.amountB = 0
this.capitalA = 0
this.capitalB = 0
}
这是我控制台上的错误:Console error
谁能帮我解决这些错误。谢谢!
【问题讨论】:
标签: javascript typescript vue.js vuejs2