properties之带有下划线的 Vue 属性无效

Leo_wl 阅读:186 2025-01-19 22:14:33 评论:0

我正在做一些测试,我注意到当我使用下划线时我的属性无效。

例子:

new Vue({ 
el : "#form", 
 
data: { 
    errors: [], 
    _username: '', 
    _password: '' 
}); 

在 html 文件中:
<input class="uk-input" type="text" v-model="_username" > 
<input class="uk-input" type="password" v-model="_password"> 

使用上面的代码,应用程序将不会呈现。如果我删除下划线它会起作用,有人知道为什么会发生这种情况吗?

请您参考如下方法:

答案可以在 documentation 中找到。

Properties that start with _ or $ will not be proxied on the Vue instance because they may conflict with Vue’s internal properties and API methods. You will have to access them as vm.$data._property



在您的模板中,您必须引用 $data._username/ $data._password ,例如
<input class="uk-input" type="text" v-model="$data._username" > 
<input class="uk-input" type="password" v-model="$data._password"> 

演示在这里~ https://jsfiddle.net/9bzxuecj/2/


标签:VUE
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号