Solves the problem of Bidirectional binding between the Vue2.x parent component and the child component, and binds the vue2.x component

Source: Internet
Author: User

Solves the problem of Bidirectional binding between the Vue2.x parent component and the child component, and binds the vue2.x component

Recently, I have been studying how to write a Vue2.x-based UI component for myself to Improve the efficiency by a little BIG. I encountered a problem when creating components containing input: I don't know how to bind the input and caller (parent component) data in the child component in two ways. I thought about using Vuex, but I watched 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 the experts, I finally found it.

We will post the solution here, hoping to help our colleagues who are new to the Vue framework.

Code logic of child components

<Template> <div class = "ne-ept"> <input type = "text" v-model = "currentValue"> </div> </template> <style lang = "less" scoped> @ import ".. /.. /assets/styles/form. less "; </style> <script> export default {name: 'neipt', props: {// receives a value passed by the parent component: {type: string, default: function () {return ''}}, computed: {currentValue: {// get: function () {return this. value; // assign the value in props to currentValue}, set: function (val) {this. $ emit ('input', val); // trigger the parent component through $ emit }}</script>

Code logic of the parent component

<Template> <div id = "button-index"> <ne-EPT placeholder = "name" v-model = "test"> </ne-EPT> </div> </template> <style> </style> <script> import NeIpt from '. /neip' export default {name: 'form-Index', data () {return {test: ''}}, components: {neip}}</script>

When you modify the currentValue of the child component, you can use $ emit to trigger the input event and pass the value to the caller's v-model to implement bidirectional binding.

Summary

The above section describes how to solve the bidirectional binding problem between the Vue2.x parent component and the child component. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.