Vue parent child component communication between components

  • 2021-08-03 08:26:42
  • OfStack

I don't understand the communication between Vue components. I searched a lot about the communication between Vue father and son components. I will record 1 below. I need to know about Vue father and son components. Friends who communicate between components can refer to it. I hope this article will be helpful to you.


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title> Component </title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <script src="../js/Vue.js"></script>
  <script>
    window.onload = function(){
      var vm = new Vue({
        el:'#box',
        data:{},
        components:{
          aaa:{
            template:'<h2> This is aaa Component <bbb></bbb></h2>',
            components:{
              bbb:{
                template:'<h2> This is bbb Component </h2>'
              }
            }
          }
        }
      });
    }
  </script>
</head>
<body>
<div id="box">
  <aaa></aaa>
</div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="../js/vue1.0.js"></script>
  <script>
    // The child component gets the value of the parent component 
    window.onload = function () {
      var vm = new Vue({
        el: '#box',
        data: {
          a: 'aaa'
        },
        components: {
          'aaa': {
            data(){
              return {
                msg: 111,
                msg2: ' This is the data of the parent component '
              }
            },
            template: '#aaa',
            components: {
              'bbb': {
                //html In my-msg This form ,js Hump should be named in myMsg
                props: ['mmm', 'myMsg'],
                template: '<h3> I am bbb Component ->{{mmm}}<br>{{myMsg}}</h3>'
              }
            }
          }
        }
      });
    }
  </script>
</head>
<body>
<div id="box">
  <aaa>
  </aaa>
</div>

<template id="aaa">
  <h1>11111</h1>
  <bbb :mmm="msg2" :my-msg="msg"></bbb>
</template>
</body>
</html>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="../js/vue1.0.js"></script>
  <script>
    // The child component gets the value of the parent component 
    window.onload = function () {
      var vm = new Vue({
        el: '#box',
        data: {
          a: 'aaa'
        },
        components: {
          'aaa': {
            data(){
              return {
                msg: 111,
                msg2: ' This is the data of the parent component '
              }
            },
            template: '#aaa',
            components: {
              'bbb': {
                //html In my-msg This form ,js Hump should be named in myMsg
                props: ['mmm', 'myMsg'],
                template: '<h3> I am bbb Component ->{{mmm}}<br>{{myMsg}}</h3>'
              }
            }
          }
        }
      });
    }
  </script>
</head>
<body>
<div id="box">
  <aaa>
  </aaa>
</div>

<template id="aaa">
  <h1>11111</h1>
  <bbb :mmm="msg2" :my-msg="msg"></bbb>
</template>
</body>
</html>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title> The parent component gets the value of the child component </title>
  <script src="../js/vue1.0.js"></script>
  <script>
    // The parent component gets the value of the child component 
    window.onload = function () {
      var vm = new Vue({
        el:'#box',
        data:{
          a:'aaa'
        },
        components:{
          'aaa':{
            data(){
              return {
                msg:111,
                msg2:' I am the data of the parent component '
              }
            },
            template:'#aaa',
            methods:{
              get(msg){
                this.msg = msg;
              }
            },
            components:{
              'bbb':{
                data(){
                  return {
                    a:' I am the data of the subcomponent '
                  }
                },
                template:'#bbb',
                methods:{
                  send(){
                    this.$emit('child-msg',this.a);
                  }
                }
              }
            }
          }
        }
      });
    }
  </script>
</head>
<body>
<div id="box">
  <aaa></aaa>
</div>
<template id="aaa">
  <span> I am the parent  - "  {{msg}}</span>
  <bbb @child-msg="get"></bbb>
</template>
<template id="bbb">
  <h3> Subcomponent </h3>
  <input type="button" value="send" @click="send" />
</template>
</body>
</html>

Related articles: