The method by which the parent component in Vue2.x transmits data to the child component.

Source: Internet
Author: User

The method by which the parent component in Vue2.x transmits data to the child component.

Parent Component Structure

Template

<template> <div> <v-girl-group   :girls="aGirls"></v-girl-group> </div></template>

Script

<Script> import vGirlGroup from '. /GirlGroup 'export default {name: 'girl ', components: {vGirlGroup}, data () {return {aGirls: [{name: 'lily', age: 22 }, {name: 'xiaomei ', age: 21 },{ name: 'xiaohe', age: 24}] }}</script>

Note:

• Spelling of child components: vGirlGroup is written as v-girl-group
• The data bound to the child component is girls = "aGirls", where aGirls data is the data in the parent component, and girls is the attribute to be passed to the child component.

Child Component Structure

Template

<template> <div> <ul>  <li v-for="(value, index) in girls">{{ index }} - {{ value.name }} - {{ value.age }}</li> </ul> </div></template>

Note:

• V-for Parameter order when traversing objects-for details, refer to: https://cn.vuejs.org/v2/guide/migration.html?v-for-sequence of Parameter order when traversing objects-change
• Remove the $ index and $ key implicitly declared variables in v-for. for details, see https://cn.vuejs.org/v2/guide/migration.html?index-and-key-remove.

<script>export default { name: 'girl-group', props: { girls: {  type: Array,  required: true } }}</script>

Note:

• The data in props comes from the value bound to the child component in the parent component. In addition, when developing with IDE and editor, a prompt may be displayed as prop.
• Perform some validation on the data in girls.

Result

The above section describes how to transmit the data of the parent component in Vue2.x to the child component, hoping to help you. If you have any questions, please leave a message, the editor will reply to you in time!

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.