核心方法
$emit
和$on
Component One
<script>
import Bus from 'Bus'
export default {
methods: {
listen () {
Bus.$on('event-name', function (data) {
// 处理数据
})
}
}
}
</script>
Component Two
<script>
import Bus from 'Bus'
export default {
data () {
return {
data
}
},
methods: {
send() {
// 发送数据
Bus.$emit('event-name', data)
}
}
}
</script>
Bus
import Vue from 'vue'
const bus = new Vue()
export default bus