zoukankan      html  css  js  c++  java
  • Vue.js组件的通信之父组件向子父组件的通信

    <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>componentParentChildCommunication</title>
        <script src="js/vue.js"></script>
      </head>


      <template id="parentComp">
        <div>
          I am parent component:{{msg}},The Data from child:{{msg2}}
          <hr>
          <!-- <child @自定义事件名="父方法"></child> -->
          <child @child="parentFn"></child>
        </div>
      </template>

      <template id="childComp">
        <div>I am child component:{{msg}}</div>
      </template>
      <body>


      <script>
        let child={
          template:'#childComp',
          data(){
            return {
              msg:'child Data'
            }
          },
          mounted(){
          /*this.$emit('自定义事件名',数据);*/
            this.$emit('child',this.msg);
          }
        };

        let parent={
          template:'#parentComp',
          data(){
            return {
              msg:'parent Data',
              msg2:''
            }
          },
          components:{
            child
          },
          methods:{
            parentFn(data){
              this.msg2=data;
            }
          }
        };


        window.onload=function(){
          new Vue({
            el:'#app',
            components:{
              parent
            }
          });
        }
        /*父元素向子元素通信关键总结:
          1:在嵌套的子元素(使用时)上:<child @自定义事件名="父方法"></child>;
          2:子元素在加载完成的钩子函数(mounted)上加一个方法:this.$emit('自定义事件名',数据);
          3:父元素上的方法:父方法名(data){...}
        */
      </script>


      <div id="app">
        <parent></parent>
      </div>
      </body>
    </html>

    焦大叔叔
  • 相关阅读:
    CSS基础教程要点笔记
    Python编程入门-第六章 字符串 -学习笔记
    Swift游戏实战-跑酷熊猫 08 产生源源不断的移动平台
    Swift游戏实战-跑酷熊猫 07 平台的移动
    ANE-IOS与AS的互通
    Swift游戏实战-跑酷熊猫 06 创建平台类以及平台工厂类
    Swift游戏实战-跑酷熊猫 05 踩踏平台是怎么炼成的
    构建针对 iOS 和 Android 的原生扩展
    ANE打包心得
    Swift游戏实战-跑酷熊猫 04 熊猫的跳和滚的动作
  • 原文地址:https://www.cnblogs.com/tiny-jiao/p/6527392.html
Copyright © 2011-2022 走看看