<input v-model="parentMsg">
, the subassembly can be achieved to the bidirectional data in the parent data binding component:
Let me talk about the use of v-model:
Parent component:
<div> <input type="text" v-model='msg'> <child v-model='msg'></child> </div>
Subassembly:
Vue.component('child', { props: ['value'], template: '<input type="text" @input="handleInput" :value=value />', methods: { handleInput(e){ console.log(e); this.$emit('input', e.target.value); } } }) new Vue({ el:'#example', data:{ msg:'好天气', parentMsg:'' } })
Whether the input box to change the parent component or sub-component, the value will change the value and msg, the value of the two input boxes will simultaneously changed.
: difference between the model and the v-model
: model is v-bind: abbreviation of model, <child :model="msg"></child>
which simply passes the data to the parent element subassembly, and the subassembly is not binding and bidirectional data parent component. Of course, except for reference types, changing the type of sub-assembly of data references, it will change the parent component.
Original link: https://www.cnblogs.com/xuzhudong/p/8617487.html
Let me talk about the use of v-model:
Parent component:
<div> <input type="text" v-model='msg'> <child v-model='msg'></child> </div>
Subassembly:
Vue.component('child', { props: ['value'], template: '<input type="text" @input="handleInput" :value=value />', methods: { handleInput(e){ console.log(e); this.$emit('input', e.target.value); } } }) new Vue({ el:'#example', data:{ msg:'好天气', parentMsg:'' } })
Whether the input box to change the parent component or sub-component, the value will change the value and msg, the value of the two input boxes will simultaneously changed.
: difference between the model and the v-model
: model is v-bind: abbreviation of model, <child :model="msg"></child>
which simply passes the data to the parent element subassembly, and the subassembly is not binding and bidirectional data parent component. Of course, except for reference types, changing the type of sub-assembly of data references, it will change the parent component.
Original link: https://www.cnblogs.com/xuzhudong/p/8617487.html