[英]Trying to set the value of a Vuejs input computed property
我有一个使用v-model方法绑定到VueJ的字段的表单。 当用户从下拉列表中选择项目时,将从$http
响应中填充字段。
HTML:
<form id="vApp">
<input type="number" v-model="hourlyRate">
</form>
JavaScript的:
var thing = new Vue({
el: '#vApp',
data: {
response: {}
},
computed: {
hourlyRate: function(){
return this.response.length > 0 ? 0 : this.response.hourlyRate;
}
},
methods: {
getHourlyRate: function(){
this.$http.post('someUrl', {iWant: 'hourlyRate'},
function( response ){
this$set('response', response);
}
}
}
});
因此,用户可以基于其下拉菜单获得输入的“罐头”选项,但我也希望他们能够输入一个值,并将该值变成对象的hourlyRate
值。
您不能像这样修改计算属性。 只需创建hourlyRate属性并在收到响应时进行设置即可:
var thing = new Vue({
el: '#vApp',
data: {
hourlyRate: 0
},
methods: {
getHourlyRate: function(){
this.$http.post('someUrl', {iWant: 'hourlyRate'},
function( response ){
this.hourlyRate = response.length > 0 ? 0 : response.hourlyRate;
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.