event-bus.uvue 1.1 KB
Newer Older
Q
qiang 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <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>
Q
qiang 已提交
16 17
      </view>
    </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
18 19 20
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
Q
qiang 已提交
21 22 23 24 25 26
</template>

<script lang="ts">
export default {
  data() {
    return {
DCloud-WZF's avatar
DCloud-WZF 已提交
27
      log: [] as string[],
Q
qiang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    }
  },
  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
DCloud-WZF's avatar
DCloud-WZF 已提交
48 49
    },
  },
Q
qiang 已提交
50 51 52 53 54 55 56
}
</script>

<style>
.box {
  padding: 10px;
}
DCloud-WZF's avatar
DCloud-WZF 已提交
57
</style>