Vue3.0 custom components use v-model two-way binding

When we extract an input search field, we need to bind data in the parent component two-way. So what should we do? 3.0vue has this way to help me achieve

1. The parent component
Father.vue

 <Input @send="getChildren" v-model:keyword="keyword"></Input >
data() {
    
    
     return {
    
    
        keyword:''
      }
   },

2. Sub-component
Input.vue

 <input type="text" 
 :value="keyword" 
 placeholder="请输入内容" 
 @input="$emit('update:keyword',$event.target.value)" //该方法可以同步实现父子组件的数据变化
 @keyup.enter="change" />

 props: {
    
    
    //接收数据属性
  keyword:{
    
    
    type:String,  
    default:'',
    request:true
      }
  },

methods:{
    
    
 change(){
    
    
   console.log(this.keyword)//获得修改后的数据  可以在次定义事件函数 发送请求
  }
}

Guess you like

Origin blog.csdn.net/qq_45557681/article/details/114976109