event-bus.uvue 992 字节
Newer Older
Q
qiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<template>
  <view>
    <button @click="on">开始监听</button>
    <button @click="once">监听一次</button>
    <button @click="off">取消监听</button>
    <button @click="emit">触发监听</button>
    <button @click="clear">清空消息</button>
    <view class="box">
      <view>收到的消息:</view>
      <view>
        <view v-for="(item, index) in log" :key="index">{{ item }}</view>
      </view>
    </view>
  </view>
</template>

<script lang="ts">
export default {
  data() {
    return {
      log: [] as string[]
    }
  },
  methods: {
    fn(res: string) {
      this.log.push(res)
    },
    on() {
      uni.$on('test', this.fn)
    },
    once() {
      uni.$once('test', this.fn)
    },
    off() {
      uni.$off('test', this.fn)
    },
    emit() {
      uni.$emit('test', 'msg:' + Date.now())
    },
    clear() {
      this.log.length = 0
    }
  }
}
</script>

<style>
.box {
  padding: 10px;
}
</style>