vue-emitter-bus
v1.0.3
Published
EventBus for vue3.
Downloads
7
Readme
vue-emitter-bus
EventBus for Vue3.
具体用法请参考
mitt
main.js
import {createApp} from 'vue'
import App from './App.vue'
import {EmitterBus} from 'vue-emitter-bus'
const app = createApp(App)
.use(EmitterBus)
// 或者
// .use(EmitterBus, {propertyName: '$bus'})
app.mount('#app')
1、当前 Vue
实例直接使用
this.$copyText('')
<script>
export default {
name: "Test",
created() {
// listen to an event
this.$bus.on('foo', e => console.log('foo', e) )
// listen to all events
this.$bus.on('*', (type, e) => console.log(type, e) )
// fire an event
this.$bus.emit('foo', { a: 'b' })
// clearing all events
this.$bus.all.clear()
// working with handler references:
function onFoo() {}
this.$bus.on('foo', onFoo) // listen
},
unmounted () {
this.$bus.off('foo', onFoo) // unlisten
}
methods: {}
}
</script>
2、单独使用
import {emitter} from "vue-emitter-bus"
// listen to an event
emitter.on('foo', e => console.log('foo', e) )
// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )
// fire an event
emitter.emit('foo', { a: 'b' })
// clearing all events
emitter.all.clear()
// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo) // listen
emitter.off('foo', onFoo) // unlisten