如何使用Vue.js将方法结果绑定到v模型?
例如:
<someTag v-model="method_name(data_attribute)"></someTag>
Run Code Online (Sandbox Code Playgroud)
我出于某种原因无法使其发挥作用.
谢谢.
gre*_*r69 32
多年后,随着更多的经验,我发现绑定:value比使用 v-model更容易。然后您可以通过捕获来处理更新@change。
Jef*_*eff 21
v-model表达式必须具有get和set函数.对于大多数变量,这非常简单,但您也可以使用计算属性来自己定义它们:
data:function(){
return { value: 5 }
},
computed: {
doubleValue: {
get(){
//this function will determine what is displayed in the input
return this.value*2;
},
set(newVal){
//this function will run whenever the input changes
this.value = newVal/2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用 <input v-model="doubleValue"></input>
如果您只想让标签显示方法结果,请使用 <tag>{{method_name(data_attribute)}}</tag>
| 归档时间: |
|
| 查看次数: |
18358 次 |
| 最近记录: |