简体   繁体   English

如何在Vue.js中调整文本字段反应性

[英]How to adjust text field reactivity in Vue.js

I have a simple text field in Vue.js: 我在Vue.js中有一个简单的文本字段:

 new Vue({ el: '#app', data: { value: 'Test' } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <input type="text" v-model="value" ref="input" /> <p>{{ value }}</p> </div> 

Is it possible to decrease the frequency of updating the value in the data model from 'onChange' to 'onBlur'? 是否可以减少将数据模型中的值从“ onChange”更改为“ onBlur”的频率?

v-model is just syntax sugar for => v-model只是=>的语法糖

:value="modelValue" @input="modelValue = $event.target.value" :value =“ modelValue” @ input =“ modelValue = $ event.target.value”

If you want something else, it's very easy to do. 如果您还想要其他东西,那很容易做到。 Just change the update side to onBlur, so => 只需将更新侧更改为onBlur,所以=>

<input  class="form-control
    :value="value" 
    @blur="value = $event.target.value"
    @input="value = $event.target.value"
>

The improved example code: 改进的示例代码:

 new Vue({ el: '#app', data: { value: 'Test' } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <input type="text" :value="value" @blur="value = $event.target.value" ref="input" /> <p>{{ value }}</p> </div> 

Yes, you should Just add @blur event, and pass through it the value of the event 是的,您应该只添加@blur事件,并通过它传递事件的值

Then when this event gets triggered in methods , it will change the value of result to the input value...so the updating became only conditioned to blur of the input 然后,在methods触发此事件时,它将result的值更改为输入值...因此,更新仅取决于输入的blur

 new Vue({ el: '#app', data: { result: '', value:'' }, methods:{ blo(e){ this.result = e } } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div id="app"> <input type="text" @blur='blo(value)' v-model="value" /> <p>{{ result }}</p> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM