Vue2.0 sub-component to change the props value and pass the value to the parent component, vue2.0props

Source: Internet
Author: User

Vue2.0 sub-component to change the props value and pass the value to the parent component, vue2.0props

Why are we impulsive to modify data in prop? There are two reasons:

PropAfter being passed in as an initial value, the child component wants to use it as local data;

PropInput as an initial value, which is processed by the Child component into other data output.

For these two reasons, the correct response is:

Define a local variable and initialize it with the value of prop:

props: ['initialCounter'],data: function () { return { counter: this.initialCounter }}

Define a computing attribute, process the value of prop and return it.

props: ['size'],computed: { normalizedSize: function () { return this.size.trim().toLowerCase() }}

Note that objects and arrays in JavaScript are reference types and point to the same memory space. If prop

Is an object or array. Changing it within the child component will affect the status of the parent component.

We know that the parent component uses props to pass data to the child component, but how does the child component communicate with the parent component? At this time, the Custom Event System of Vue comes in handy. Https://cn.vuejs.org/v2/guide/components.html#custom event

Define two variables in the parent component and dynamically bind them to the child component.

Child components:

Parent component:Define two methods and bind them to custom events of child components

The method for changing the props value of the vue2.0 sub-component and passing the value to the parent component is to share all the content with you. I hope to give you a reference, we also hope that you can support the customer's home.

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.