event-bus.uvue 1.2 KB
Newer Older
Q
qiang 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2
  <!-- #ifdef APP -->
H
hdx 已提交
3 4
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7 8 9 10 11 12 13 14 15
    <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>
H
hdx 已提交
18
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
19 20
  </scroll-view>
  <!-- #endif -->
Q
qiang 已提交
21 22
</template>

H
hdx 已提交
23 24 25 26 27 28
<script>
  export default {
    data() {
      return {
        log: [] as string[],
      }
Q
qiang 已提交
29
    },
H
hdx 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    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 已提交
49
    },
H
hdx 已提交
50
  }
Q
qiang 已提交
51 52 53
</script>

<style>
H
hdx 已提交
54 55 56 57
  .box {
    padding: 10px;
  }
</style>