Continue learning with a detailed explanation of the communication between the parent and child of the vue component. This article mainly introduces in detail the information related to the communication between children and parents between Vue components. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
2. Communication between components (child components pass values to parent components)
Complete data transmission through events.
①Define a method in the parent component to receive the value passed by the child component through the event
##
methods:{ recvMsg:function(msg){ //参数msg就是子组件通过事件出来的数据 } }
<child-component @myEvent="recvMsg"></child-component>
事件名,值 this.$emit('myEvent',myPhone) //触发一个叫做myEvent的事件,同时把第二个参数数据传递给事件对应的处理函数
Summary:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>组件间通信子传父</title> <script src="js/vue.js"></script> </head> <body> <p id="container"> <p>{{msg}}</p> <parent-component></parent-component> </p> <script> //通过事件的方式传递 // 绑定 -- 触发 Vue.component("parent-component",{ data:function(){ return { sonMsg:"" } }, methods:{ //msg参数要拿子传递的值 recvMsg:function(msg){ console.log("父组件接收到子组件的数据"+msg); this.sonMsg = msg; } }, template:` <p> <h1>这是父组件</h1> <p>子组件传来的数据为:{{sonMsg}}</p> <hr/> <child-component @customEvent="recvMsg"></child-component> </p> ` }) Vue.component("child-component",{ methods:{ sendMsg:function(){ //来触发绑定给子组件的自定义方法 //this.$emit("customEvent");第一个参数触发 //this.$emit("customEvent");第二个参数传值 this.$emit("customEvent","哈哈哈哈"); }, }, template:` <p> <h1>这是子组件</h1> <button @click="sendMsg">senToFather</button> </p> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>子与父之间的通信</title> <script src="js/vue.js"></script> </head> <body> <p id="container"> <p>{{msg}}</p> <parent-component></parent-component> </p> <script> //创建父组件 Vue.component("parent-component",{ //data属性 data:function(){ return{ sonMsg:"" } }, methods:{ recvMsg:function(msg){ this.sonMsg = msg } }, template:` <p> <h1>父组件</h1> <h4>子组件传递的数据:{{sonMsg}}</h4> <child-component @customEvent="recvMsg"></child-component> </p> ` }) //创建子组件 Vue.component("child-component",{ data:function(){ return { myInput:"" } }, methods:{ sendMsg:function(){ this.$emit("customEvent",this.myInput); } }, template:` <p> <h1>子组件</h1> <input type="text" v-model="myInput"/> <button @click="sendMsg">发送</button> </p> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
Detailed explanation of parent-child communication in the vue component (1)
Introduction and use of the v for directive in the vue component v- Analysis of alarm issues when for
Examples of methods for using iframe elements in vue components
The above is the detailed content of Detailed example of communication between vue components Detailed explanation of child and parent (2). For more information, please follow other related articles on the PHP Chinese website!