事件总线bus解决兄弟组件之间的传值

原文引用https://www.dazhuanlan.com/2019/08/25/5d625951eff92/


事件总线bus解决兄弟组件之间的传值

实际运用:

封装一个Bus.js

1
2
3
import Vue from 'vue'
const Bus = new Vue()
export default Bus

在组件调用时引入

组件一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import Bus from './Bus'

export default {
data() {
return {
.........
}
},
methods: {
....
Bus.$emit('log', 120)
},

}

组件二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import Bus from './Bus'

export default {
data() {
return {
.........
}
},
mounted () {
Bus.$on('log', content => {
console.log(content)
});
}
}

猜你喜欢

转载自www.cnblogs.com/petewell/p/11408835.html