vue filled pit of use: the difference between the model and the v-model

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: 

 

Copy the code
 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:''
    }
   })  
Copy the code

 

 

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: 

 

Copy the code
 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:''
    }
   })  
Copy the code

 

 

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

Guess you like

Origin www.cnblogs.com/baibabai/p/11286829.html