zoukankan      html  css  js  c++  java
  • Vue通讯

    vue组件通讯

    #props传递数据

    • 父->子
    //父组件传递数据
    <template>
      <Children :data="msg"></Children>
    </template>
    
    <script>
    import Children from "./Children";
    export default {
     components: {
      Children
     },
     data() {
      return { msg: 100 };
     }
    };
    </script>
    
    //子组件接收数据
    <script>
    export default {
        props: {
        name: {
            type: Number,               //传递数据类型,不符合会发出警告
          default: ()=>{}           //指定默认值
        }
      }
    };
    </script>

    #$emit

    • 子-父  $emit使用
    //子组件   使用$emit自定义事件
    <template>
      <div @click="handler()">向父组件传递数据</div>
    </template>
    
    <script>
    export default {
        methods: {
        handler () {
            this.$emit('change', '我是向父组件传递的数据');
        }
      }
    };
    </script>
    
    //父组件
    <template>
      <Children @change="receive()"></Children>
    </template>
    
    <script>
    import Children from "./Children";
    export default {
     components: {
      Children
     },
     methods: {
        receive (res) {
        console.log('res:' + res);
      }
     }
    };
    </script>

    #.sync

    • .sync
    用法: 改变子组件或者父组件传递的值。
    
    //父-子  子组件接收参考上面
    <template>
      <Children :data.sync="msg"></Children>
    </template>
    
    //子->父
    <template>
      <div @click="$emit('update:name','向父组件传递的数据')"></div>
    </template>
    
    <template>
        {{ name }}
      <Children :name.sync="name"></Children>
    </template>
    <script>
    import Children from "./Children";
    export default {
     components: {
      Children
     },
     data () {
        name: '默认数据'
     }
    };
    </script>

    #$parent与$children

    • 爷孙组件,this.$parent, this.$children
    //向上派发事件
    Vue.prototype.$dispatch = function $dispatch(eventName, data) {   
      let parent = this.$parent;
      while (parent) {
        parent.$emit(eventName, data);
        parent = parent.$parent;
      }
    };
    
    this.$parent.$emit('change', '调用父组件的emit事件向上派发');
    
    
    //向下派发事件
    Vue.prototype.$broadcast = function $broadcast(eventName, data) {
      const broadcast = function () {
        this.$children.forEach((child) => {
          child.$emit(eventName, data);
          if (child.$children) {
            $broadcast.call(child, eventName, data);
          }
        });
      };
      broadcast.call(this, eventName, data);
    };
    
    this.$children 访问子组件
    

    #兄弟组件通信(EventBus)

    Vue.prototype.$bus = new Vue();
    
    //兄弟一
    <div @click="handler()"></div>
    <script>
    export default {
     methods: {
        handler () {
        this.$bus.$emit('send', '我是传递的数据');
      }
     }
    };
    </script>
    
    //兄弟二
    <script>
    export default {
     methods: {
        handler () {
        let _self = this;
        _self.$bus.$on('send', (res) => {
            cosole.log('msg:' + msg);
        });
      }
     }
    };
    </script>

    #Provide与inject

    //在父组件中注入数据
    provide() {
      return { parentMsg: "父亲" };
    }
    
    //在任意子组件中可以注入父级数据
    inject: ["parentMsg"] // 会将数据挂载在当前实例上

    #$attrs

    //父组件
    <template>
      <Children name="along"></Children>
    </template>
    
    //子组件
    <template>
        {{ $attrs }}
        //{ "msg": "along" }
    </template>
  • 相关阅读:
    UE4 WCF RestFul 服务器 读取JSON 数据并解析 简单实例
    Android aidl Binder框架浅析
    AIDL
    android 五种存储方式
    Android进程间通信机制
    Service全面总结
    Android平台中关于音频播放
    Android广播机制
    Cursor,CursorAdapter中的观察者模式解析
    ContentProvider和Uri详解
  • 原文地址:https://www.cnblogs.com/alongup/p/12111989.html
Copyright © 2011-2022 走看看