bus.js
import Vue from ‘vue‘
export default new Vue
App.vue
<template>
<div id="app">
<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>
原文地址:https://www.cnblogs.com/xl4ng/p/12593581.html
时间: 2024-11-03 22:44:04