Vue组件通信方式

5 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue组件通信方式</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
  <child-a class="child-a" v-bind:name="name" :skt="lol" scool="www" style="border: 1px green solid" @fire="onFire"
           @click.native="handleClick">
  </child-a>
</div>
<script>
/**
 * Vue组件通信方式
 * 组件关系 root -> child-a -> child-b、child-c
 * 1: $emit、v-on、$props 父子通讯
 * 2: $emit、$on 事件总线,任意组件间通讯
 * 3: $attrs、$listeners 父组件和后代组件通讯
 * 4: ref、$children、$parent 可以调用父子组件的属性和方法,ref可以获取DOM实例或组件实例
 * 5: inject、provide 祖先组件向其所有子孙后代注入一个依赖
 * 6: vuex、路由、本地存储也可以进行组件间通信
 * */

// 事件总线
Vue.prototype.bus = new Vue();

Vue.component("childA", {
  inheritAttrs: true, // 是否显示组件上的属性,(排除class、style和被props接收的属性)
  provide: {
    pre: 'hello world'
  },
  props: {
    name: {
      type: String,
      defalut: ""
    }
  },
  data: function() {
    return {
      message: "childA message",
      code: "1001",
      last: "lastName"
    };
  },
  template: "" +
    "<div>{{message}}" +
    "<p ref='Ap'>{{name}} {{$parent.start}}</p>" +
    "<child-b v-bind='$attrs' v-on='$listeners' ref='Ab'></child-b>" +
    "<child-c v-bind='$attrs' v-on='$listeners' ref='Ac' class='child-c-class' style='color: red' first='firstName' :last='last'></child-c>" +
    "<button @click='handleClick'>handleAClick</button>" +
    "</div>",
  methods: {
    handleClick: function() {
      this.$emit("fire", this.code);
    }
  },
  mounted: function() {
    console.log("childA $parent", this.$parent);
    console.log("childA $children", this.$children);
    console.log("childA $refs", this.$refs);

    console.log("childA $attrs", this.$attrs);
    console.log("childA $listeners", this.$listeners);
  }
});

Vue.component("childB", {
  data: function() {
    return {
      message: "childB message",
      code: "1002"
    };
  },
  template: "" +
    "<div>{{message}}" +
    "<button @click='handleClick'>handleBClick</button>" +
    "</div>",
  methods: {
    handleClick: function() {
      this.bus.$emit("bus", this.code);
    }
  },
  mounted: function() {
    console.log("childB $attrs", this.$attrs);
    console.log("childB $listeners", this.$listeners);
  }
});

Vue.component("childC", {
  props: {
    last: {
      type: String,
      default: ''
    }
  },
  data: function() {
    return {
      message: "childC message"
    };
  },
  template: "<div>{{message}} " +
    "<child-d v-bind='$attrs' v-on='$listeners'></child-d>" +
    "</div>",
  beforeCreate: function() {
    this.bus.$on("bus", function(e) {
      console.log("on bus", e);
    });
  },
  mounted: function() {
    console.log("childC $attrs", this.$attrs);
    console.log("childC $listeners", this.$listeners);
  }
});

Vue.component("childD", {
  inject: {
    // she: {
    //   from: 'pre',
    //   default: 'she !'
    // },
    pre: {
      default: 'she !'
    }
  },
  data: function() {
    return {
      message: "childD message"
    };
  },
  template: "<div>{{message}}</div>",
  mounted: function() {
    console.log("childD $listeners", this.$listeners);
    console.log("childD $attrs", this.$attrs);

    console.log("childD pre", this.pre);
  }
});

var app = new Vue({
  el: "#app",
  data: {
    messages: "hello vue !",
    name: "name !",
    start: "start !",
    lol: "lol !"
  },
  methods: {
    onFire: function(e) {
      console.log("onFire", e);
    },
    handleClick: function(e) {
      console.log("app handleClick", e);
    }
  }
});
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值