[英]Vue JS - vue-class-component binding input
I'm currently working on a Vue 2 project with TS using these libraries for component setup: vue-class-component
and vue-property-decorator
我目前正在使用 TS 开发 Vue 2 项目,使用这些库进行组件设置:
vue-class-component
和vue-property-decorator
I need to create a form component, but I'm having a hard time with data binding, because when I use the data() option for my form component it produces errors on compile time.我需要创建一个表单组件,但是我很难处理数据绑定,因为当我为表单组件使用 data() 选项时,它会在编译时产生错误。
Is there a way to bind input data without using the data() option?有没有办法在不使用 data() 选项的情况下绑定输入数据?
The errors I get:我得到的错误:
ERROR in /path/to/components/folder/Form.vue
Property 'id' does not exist in type 'Form'.
For more context here's my code:有关更多上下文,这是我的代码:
@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
}
And here's the error on my console: Console error这是我控制台上的错误:控制台错误
Can anyone helps me to get ride of these error.任何人都可以帮助我解决这些错误。 Thank you!
谢谢!
In Vue class component decorators, the Options API's data()
function is abstracted away.在 Vue class 组件装饰器中,Options API 的
data()
function 被抽象掉了。
The @Component
decorator is a function responsible for transpiling your code from Class API syntax into Options API syntax. @Component
装饰器是一个 function 负责将您的代码从 Class API 语法转换为选项 ZDB9742CE38714CA8DE634A 语法。
Namely, behind the scenes:即,在幕后:
data()
reactive props,data()
反应道具,computed
propscomputed
的 propsmethods
.methods
。 So just declare the props directly on the class instance:所以直接在 class 实例上声明 props:
@Component
export default class Form extends Vue {
id = "";
name = "";
age = 0;
amountA = 0;
amountB = 0;
capitalA = 0;
capitalB = 0;
// props declared above are reactive and you can now use them
// in getters or methods
}
See the example in the Overview page.请参阅概览页面中的示例。 In more detail, this particular comment:
更详细地说,这个特定的评论:
// Class properties will be component data
// Class 属性将是组件数据
Warning: If you're just picking up Vue, be warned Vue class decorators syntax is on a downwards usage trend and problems with Vue 3 compatibility are to be expected (and have already started to surface).警告:如果您刚刚开始使用 Vue,请注意 Vue class 装饰器语法的使用趋势正在下降,并且 Vue 3 兼容性问题是可以预料的(并且已经开始浮出水面)。 Have a look at the open issues on their repo.
看看他们回购的未解决问题。
On the same note, you might want to read this comment by Evan You 1 , ref usage of Vue class component decorators:同样,您可能想阅读 Evan You 1的此评论,参考 Vue class 组件装饰器的用法:
It definitely isn't a recommended option anymore in official docs or default tooling (moving to Vite-based soon and vue-cli will be in maintenance mode).
在官方文档或默认工具中,它绝对不再是推荐的选项(很快就会迁移到基于 Vite 并且 vue-cli 将处于维护模式)。
Deprecation will depend on actual usage and @ktsn 's 2 intention on further working on the project.弃用将取决于实际使用情况和@ktsn 2对进一步处理该项目的意图。
1 :: author of Vue 1 :: Vue的作者
2 :: maintainer of vue-class-component
package 2 ::
vue-class-component
package 的维护者
Personal advice: installing @vue/composition-api in your Vue 2 project and rewriting its components in Composition API syntax would be more beneficial than writing in class decorators syntax, to both yourself and the project.个人建议:在你的 Vue 2 项目中安装@vue/composition-api并用 Composition API 语法重写它的组件比用 class 装饰器语法编写对你自己和项目更有益。
In doing this, you'll在执行此操作时,您将
Because the class decorators syntax is dropping in popularity, the support for it might become more difficult to obtain, as time passes.由于 class 装饰器语法的流行度正在下降,随着时间的推移,对它的支持可能会变得更加难以获得。
If rewriting existing components in Composition API is not an option, you could at least write new components in this syntax, as the syntax is incrementally adoptable, it can be used alongside any other valid Vue API syntax.如果在 Composition API 中重写现有组件不是一个选项,您至少可以使用此语法编写新组件,因为该语法可以逐步采用,它可以与任何其他有效的 Vue API 语法一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.