@h5plus/bus

基于Vue的事件总线

Usage no npm install needed!

<script type="module">
  import h5plusBus from 'https://cdn.skypack.dev/@h5plus/bus';
</script>

README

bus

基于Vue的事件总线

安装

npm install @h5plus/bus --save
# or
yarn add @h5plus/bus

使用

import Vue from 'vue'
import bus from '@h5plus/bus'
Vue.use(bus)

示例

export default {
  name: 'home',
  bus: {
    // 自定义事件
    on: {
      test() {
        this.count++
        this.clg(this.count)
      }
    },
    // 自定义事件,但是只触发一次
    once: {
      onceTest(o) {
        this.msg = o.text
        this.clg(this.msg)
      }
    }
  },
  data() {
    return {
      msg: '',
      count: 0
    }
  },
  mounted() {
    // 触发总线上的事件
    this.$bus.emit('test')
    this.$bus.emit('onceTest', { text: 'onceTest abc ' + this.count })
  },
  methods: {
    clg(m = '') {
      console.log('[' + new Date().toLocaleTimeString() + '] ' + m)
    }
  }
}