Home  >  Article  >  Web Front-end  >  How to communicate between parent and child components in Vue? Let’s talk about the methods of passing on from father to son and from son to father.

How to communicate between parent and child components in Vue? Let’s talk about the methods of passing on from father to son and from son to father.

青灯夜游
青灯夜游forward
2021-11-30 19:41:402840browse

How to communicate between parent and child components in Vue? The following article will take you to understand the communication between Vue parent and child components, and introduce the methods of parent passing value to child and child passing value to parent. I hope it will be helpful to everyone.

How to communicate between parent and child components in Vue? Let’s talk about the methods of passing on from father to son and from son to father.

Vue parent-child component

What is a parent-child component?

Introduce another component into a component, and the introduced component is called a sub-component. Because of the modularization of Vue, the common parts will be extracted into a separate module, and all page content will not be included. Written in a vue file, due to modularity, communication problems between the two modules cannot be avoided. At this time, there is the problem of data transfer between modules (components). [Related recommendations: "vue.js Tutorial"]

Vue parent-child component communication

In vue, one component often uses the data of another component or Method, at this time there is a communication problem between parent and child components

Vue parent-to-child

1. Look at the code first, and explain it below

<body>
//父组件
<div id="app">
    <cpn3 :ctitle="title"></cpn3>
</div>
//子组件
<template id="cpn3">
    <div>
        <h1>{{ctitle}}</h1>
        <p>orange</p>
    </div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    //子组件
    Vue.component("cpn3", {
        template: "#cpn3",
        // 组件里的data是一个函数,必须要返回一个对象,变量写在返回对象里
        props: {
            ctitle: {
                type: String,
                default () {
                    return {}
                }
            }
        }
    })
	//父组件
    const app = new Vue({
        el: "#app",
        data: {
            title: &#39;orange&#39;
        },
        methods: {},

    })
</script>
</body>

Explanation:
1. First write the basic template separation component

2. Add props attributes to the sub-component in object mode. In the props, you can write the parameters you need to pass. The parameters are also in object mode. Code It’s clearer,

    //子组件
    Vue.component("cpn3", {
        template: "#cpn3",
        // 组件里的data是一个函数,必须要返回一个对象,变量写在返回对象里
        props: {
            ctitle: {
            //参数也使用对象形式,type传类型  default函数,是在找不到参数时返回一个值显示
                type: String,
                default () {
                    return {}
                }
            }
        }
    })

2. Then add the required attributes to the parent component

//父组件
    const app = new Vue({
        el: "#app",
        data: {
        //这里的title就是要传入子组件的属性  在父组件中定义
            title: &#39;orange&#39;
        },
        methods: {},

    })

3. Bind two parameters to the parent component

 <div id="app">
	//可以理解为将父组件中title 赋值给 ctitle,这样子组件就可以使用父组件的data属性了
    <cpn3 :ctitle="title"></cpn3>
</div>

4. Finally In the sub-component, use the property name defined by the sub-component

//子组件
<template id="cpn3">
    <div>
    //这里的属性名为ctitle
        <h1>{{ctitle}}</h1>
    </div>
</template>

Vue Pass from son to parent

1. Pass from son to parent means,, sub-component The content is passed to the parent component, so that the parent component can use the data from the child component at any time. The usage method is: Custom function

#1. First, let’s make an imitation Taobao sidebar product. Click or move the mouse to the above to display the content product case

How to communicate between parent and child components in Vue? Let’s talk about the methods of passing on from father to son and from son to father.

2. Taking the above picture as an example, let’s talk about the child-to-father transmission of components

2.1 Let’s separate the sub-component and the parent component. Let’s talk about them one by one. Let’s first look at the sub-component code

<!-- 子组件 -->
<template id="cpn">
    <div>
        <h1>{{ctitle}}</h1>
        <button v-for="item in list" @click="goodsclick(item)">{{item.name}}</button>
    </div>
</template>
<script>
Vue.component("cpn", {
        template: &#39;#cpn&#39;,
        //父传子 props   
        props: {
            ctitle: {
                type: String,
                //找不到数据时
                default () {
                    return {}
                }
            }
        },
        data() {
            return {
                list: [{
                    id: &#39;phone&#39;,
                    name: &#39;手机&#39;
                }, {
                    id: &#39;tv&#39;,
                    name: &#39;电视&#39;
                }, {
                    id: &#39;p&#39;,
                    name: &#39;家电&#39;
                }, {
                    id: &#39;computer&#39;,
                    name: &#39;电脑&#39;
                }, ]
            }
        },
        //子传父 自定义事件
        methods: {
            // 自定义事件
            goodsclick(item) {
                this.$emit(&#39;itemclick&#39;, item)
            }
        }
    })
    </script>

Explanation: The sub-component is to write a component (html) and wrap it up , and can be used at any time, which is equivalent to HTML, just packaged (this understanding should be no problem)

1. Subcomponents have data, methods, and attributes like vue. So write an array in data, and then use v-for to traverse and generate buttons There is no difference from normal writing, then write a function, bind the click event, and pass the item, which is the object passed in the array, into the function,

<button v-for="item in list" @click="goodsclick(item)">{{item.name}}</button>

2. Then the most important and critical step comes , that is, in the click event function you bound, send a custom function to the parent component, that is, send it to the parent component

methods: {
            // 自定义事件
            goodsclick(item) {
            //itemclick就是自定义函数,并且将item也传过去给父组件
                this.$emit(&#39;itemclick&#39;, item)
            }
        }

How to communicate between parent and child components in Vue? Let’s talk about the methods of passing on from father to son and from son to father.

#This is when the child component is written, this It is the style of the child component, as shown in the picture above

2. Next, let’s talk about how the parent component receives the custom event from the child component

<!-- 父组件 -->
<div id="app">
    <cpn :ctitle="title" @itemclick="cpnclick"></cpn>
</div>
<script>
//父组件    
    const app = new Vue({
        el: "#app",
        data: {
            title: "title",
        },
        methods: {
            cpnclick(item) {
                console.log("cpnclick", item);
            }
        }
    })
</script>

Explain: After you write the subcomponent, you need to call it. The call is just your component name as the label. Here is

<cpn :ctitle="title" @itemclick="cpnclick"></cpn>

Write a function in the parent component and bind the self passed from the subcomponent in it. Define the event, so that the sub-component is successfully bound, so that the sub-component transfers data to the parent component.

End of this chapter

For more programming-related knowledge, please visit:Introduction to Programming ! !

The above is the detailed content of How to communicate between parent and child components in Vue? Let’s talk about the methods of passing on from father to son and from son to father.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete