美文网首页
Vue2 父子组件互相传值

Vue2 父子组件互相传值

作者: Leo_23 | 来源:发表于2019-09-25 21:38 被阅读0次
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <child v-bind:msg="fatherdata" @func="getSonMsg"></child>
    </div>
    <template id="child">
      <div>
        <h1>child -- {{ msg }}</h1>
        <input
          type="button"
          value="send smg to father"
          @click="sendMsgToFather"
        />
      </div>
    </template>
    <script>
      var child = {
        template: "#child",
        props: ["msg"],
        data() {
          return {
            childmsg: "child msg"
          };
        },
        methods: {
          sendMsgToFather() {
            this.$emit("func", this.childmsg);
          }
        }
      };
      var vm = new Vue({
        el: "#app",
        data: {
          fatherdata: "123"
        },
        methods: {
          getSonMsg(data) {
            console.log(data);
          }
        },
        components: {
          child
        }
      });
    </script>
  </body>
</html>

相关文章

网友评论

      本文标题:Vue2 父子组件互相传值

      本文链接:https://www.haomeiwen.com/subject/daiwuctx.html