轻量事件总线

This commit is contained in:
2019-01-15 20:02:59 +08:00
parent a4772f5d48
commit 6132d4c0b7
4 changed files with 12 additions and 7 deletions

View File

@@ -1,8 +1,10 @@
<template>
<button @click="decrease()">decrease</button>
<button @click="decrease()">-</button>
</template>
<script>
import {EventBus} from '@/globalBus';
export default {
name: "brother",
data() {
@@ -13,7 +15,7 @@
},
methods: {
decrease() {
this.$EventBus.$emit("decreased", {
EventBus.$emit("decreased", {
num: this.num,
deg: this.deg
});

View File

@@ -1,10 +1,12 @@
<template>
<div>
<button @click="increment()">increment</button>
<button @click="increment()">+</button>
</div>
</template>
<script>
import {EventBus} from '@/globalBus';
export default {
name: "sister",
data() {
@@ -15,7 +17,7 @@
},
methods: {
increment() {
this.$EventBus.$emit("incremented", {
EventBus.$emit("incremented", {
num: this.num,
deg: this.deg
});

View File

@@ -4,7 +4,7 @@ import router from './router'
import store from './store'
Vue.config.productionTip = false
Vue.prototype.$EventBus = new Vue()
// Vue.prototype.$EventBus = new Vue()
new Vue({
router,

View File

@@ -27,6 +27,7 @@
<script>
import Brother from "../components/brother";
import Sister from "../components/sister";
import {EventBus} from '@/globalBus';
export default {
components: {
@@ -42,14 +43,14 @@
};
},
mounted() {
this.$EventBus.$on("incremented", ({num, deg}) => {
EventBus.$on("incremented", ({num, deg}) => {
this.fontCount += num
this.$nextTick(() => {
this.backCount += num
this.degValue += deg;
})
});
this.$EventBus.$on("decreased", ({num, deg}) => {
EventBus.$on("decreased", ({num, deg}) => {
this.fontCount -= num
this.$nextTick(() => {
this.backCount -= num