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;
}
}
}
v-model
表达式必须具有get
andset
函数。对于大多数变量来说,这很简单,但是您也可以使用计算属性自行定义它们,如下所示:那你可以用
<input v-model="doubleValue"></input>
如果只希望标签显示方法结果,请使用
<tag>{{method_name(data_attribute)}}</tag>
多年后,随着更多的经验,我发现绑定起来
:value
比使用v模型更容易。然后,您可以通过捕获来处理更新@change
。你的回答