Home  >  Article  >  Web Front-end  >  About two-way binding of parent component and child component in Vue2.x (detailed tutorial)

About two-way binding of parent component and child component in Vue2.x (detailed tutorial)

亚连
亚连Original
2018-06-01 10:22:401275browse

This article mainly introduces how to solve the two-way binding problem between Vue2. I am using UI components for my own use. To improve my BIG, I encountered a problem when making a component containing input: I don’t know how to achieve two-way binding between the input in the sub-component and the data of the caller (parent component). I thought about using Vuex, but I didn’t know how to use it. After looking at other excellent UI frameworks, I found that using Vuex would cause trouble to other users, so I decided to find a solution. After referring to several articles by experts, I finally found it.

I post the solution here, hoping to help colleagues like me who are coming into contact with the Vue framework for the first time.

The code logic of the child component

<template> 
 <p class="ne-ipt"> 
  <input type="text" v-model="currentValue"> 
 </p> 
</template> 
 
<style lang="less" scoped> 
 @import "../../assets/styles/form/form.less"; 
</style> 
<script> 
 export default { 
  name: &#39;NeIpt&#39;, 
  props: { 
   // 接收一个由父级组件传递过来的值 
   value: { 
    type: String, 
    default: function () { 
     return &#39;&#39; 
    } 
   } 
  }, 
  computed:{ 
   currentValue: { 
    // 动态计算currentValue的值 
    get:function() { 
     return this.value; // 将props中的value赋值给currentValue 
    }, 
    set:function(val) { 
     this.$emit(&#39;input&#39;, val); // 通过$emit触发父组件 
    } 
   } 
  } 
 } 
</script>

The code logic of the parent component

<template> 
 <p id="button-index"> 
  <ne-ipt placeholder="姓名" v-model="test"></ne-ipt> 
 </p> 
</template> 
<style> 
</style> 
<script> 
 import NeIpt from &#39;./NeIpt&#39; 
 export default { 
  name: &#39;form-index&#39;, 
  data () { 
   return { 
    test: &#39;&#39; 
   } 
  }, 
  components: { 
   NeIpt 
  } 
 } 
</script>
When modifying the currentValue of the child component, actually Triggering the input event through $emit will pass the value to the caller's v-model, thereby achieving two-way binding.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use vue element-ui ajax technologies to implement an instance of a form

vue registration component Several ways to summarize

Vue.js custom event form input component method

The above is the detailed content of About two-way binding of parent component and child component in Vue2.x (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn