bus.js

import Vue from 'vue'
export default new Vue

App.vue

<template>
  <div >
    <button @click="passMsg">传你</button>
    <my-parent></my-parent>
  </div>
</template>

<script>
import bus from "./util/bus";
import MyParent from "./views/Parent";
export default {
  components: {
    MyParent
  },
  methods: {
    passMsg() {
      bus.$emit("msg", "i am from app");
    }
  }
};
</script>
<style>
</style>

Child.vue

<template>
  <div>
    <h2>Child--{{childMsg}}</h2>
  </div>
</template>

<script>
import bus from "../util/bus";
export default {
  data() {
    return {
      childMsg: ""
    };
  },
  mounted() {
    bus.$on("msg", val => {
      this.childMsg = val;
    });
  }
};
</script>

<style>
</style>

相关文章:

  • 2021-08-15
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-05-06
  • 2021-08-26
  • 2021-04-06
猜你喜欢
  • 2022-01-20
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
  • 2022-12-23
相关资源
相似解决方案