Home > Web Front-end > JS Tutorial > Detailed example of communication between vue components Detailed explanation of child and parent (2)

Detailed example of communication between vue components Detailed explanation of child and parent (2)

小云云
Release: 2017-12-26 13:20:30
Original
1785 people have browsed it

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就是子组件通过事件出来的数据
  }
}
Copy after login

②Bind event processing function


Events are generally custom events


<child-component @myEvent="recvMsg"></child-component>
Copy after login

③Trigger events in child components


      事件名,值
this.$emit(&#39;myEvent&#39;,myPhone)
//触发一个叫做myEvent的事件,同时把第二个参数数据传递给事件对应的处理函数
Copy after login

Summary:

In Vue, the relationship between parent and child components can be summarized as props down, events up. The parent component passes data down to the child component through props, and the child component sends messages to the parent component through events.


<!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>
Copy after login

Put an input in the child component and click the button to send the content entered by the user to the parent component


<!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>
Copy after login

Related recommendations:


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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template