vue组件通信

1.父组件向子组件传值

parent.vue

<template>
  <div>
    <Child :msg="11" :fn="fn" />
  </div>
</template>

<script>
import Child from "./Child.vue";
export default {
  name: "parent",
  components: {
    Child,
  },
  methods: {
    fn(data) {
      console.log("子组件调用传过来的数据", data);
    },
  },
};
</script>

<style lang="scss" scoped></style>

child.vue

<template>
  <div>
    <h1>父组件传过来的值msg:{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  props: ["msg", "fn"],
  mounted() {
    this.fn({ name: "lisi" });
  },
};
</script>

<style lang="scss" scoped></style>

2.子组件向父组件传值

parent.vue

<template>
  <div>
    <Child @fn="handleClick" />
  </div>
</template>

<script>
import Child from "./Child.vue";
export default {
  name: "parent",
  components: {
    Child,
  },
  methods: {
    handleClick(data) {
      console.log("子组件传过来的数据", data);
    },
  },
};
</script>

<style lang="scss" scoped></style>

child.vue

<template>
  <div>
    <button @click="handleClick">点击我向父组件传值</button>
  </div>
</template>

<script>
export default {
  methods: {
    handleClick() {
      this.$emit("fn", 123);
    },
  },
};
</script>

<style lang="scss" scoped></style>

3.消息发布与订阅-pubsub-js

用于任意组件通信

npm i pubsub-js -S

parent.vue

<template>
  <div>
    <Child />
  </div>
</template>

<script>
import Child from "./Child.vue";
import pubSub from "pubsub-js";
export default {
  name: "parent",
  components: {
    Child,
  },
  mounted() {
    //订阅消息取数据
    pubSub.subscribe("one", (a, b) => {
      //参数一是事件名  参数二是传递过来的数据
      console.log("a", a);
      console.log("b", b);
    });
  },
};
</script>

<style lang="scss" scoped></style>

child.vue

<template>
  <div>
    <button @click="handleClick">点击我向父组件传值</button>
  </div>
</template>

<script>
import pubSub from "pubsub-js";
export default {
  methods: {
    handleClick() {
      //发布消息,传递数据
      pubSub.publish('one',123)
    },
  },
};
</script>

<style lang="scss" scoped></style>

4.全局事件总线

注意:最好在beforeDestroy钩子中,用$off去解绑当前组件所用到的事件

main.js注册

import Vue from 'vue'
import App from './App.vue'
// 关闭vue的生产提示
Vue.config.productionTip = false;
new Vue({
    render: h => h(App),
    // 安装全局事件总线
    beforeCreate() {
        // $bus相当于一个事件中心
        Vue.prototype.$bus = this
    }
}).$mount('#app')

提供数据

<template>
  <div class="styl">
    <button @click="sendMsg">发送数据</button>
  </div>
</template>

<script>
export default {
  methods: {
    sendMsg() {
      // 发送数据
      // 通过$emit触发send事件,发送this.name数据
      this.$bus.$emit("send", 数据);
    },
  },
};
</script>

接收数据

<script>
export default {
  name: "School",
  methods: {
    //getMsg回调函数 接收组件传过来的数据
    getMsg(data) {
      console.log('data',data)
    },
  },
  //挂载期: 自动接收
  mounted() {
    // 绑定自定义事件send,getMsg作为回调函数接收数据
    this.$bus.$on("send", this.getMsg);
  },
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值