event-bus.uvue 2.6 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
    <view>
      <button @click="on">开始监听</button>
      <button @click="once">监听一次</button>
8
      <button @click="off">取消监听</button>
9
      <!-- <button @click="offAll">取消全部监听</button> -->
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11 12 13 14 15 16
      <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>
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18 19 20 21 22
        <button @click="onObj">开始监听 obj 参数</button>
        <button @click="emitWithObj">触发监听 obj 参数</button>
        <view class="box">
          <text>接收到的 obj 参数:</text>
          <text>{{JSON.stringify(objArg)}}</text>
        </view>
23
        <button @click="testReturnId">测试返回 id</button>
Q
qiang 已提交
24 25
      </view>
    </view>
H
hdx 已提交
26
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28
  </scroll-view>
  <!-- #endif -->
Q
qiang 已提交
29 30
</template>

H
hdx 已提交
31 32 33 34 35
<script>
  export default {
    data() {
      return {
        log: [] as string[],
36
        objArg: {},
H
hdx 已提交
37
      }
Q
qiang 已提交
38
    },
H
hdx 已提交
39 40 41
    methods: {
      fn(res : string) {
        this.log.push(res)
42 43 44
      },
      fn2(res : string) {
        this.log.push(res)
H
hdx 已提交
45 46 47
      },
      on() {
        uni.$on('test', this.fn)
48 49 50 51 52
      },
      on2() {
        uni.$on('test', this.fn2)
      },
      onObj() {
DCloud-WZF's avatar
DCloud-WZF 已提交
53
        uni.$on('test-obj', (res : UTSJSONObject) => {
54 55
          this.objArg = res
        })
H
hdx 已提交
56 57 58 59 60 61
      },
      once() {
        uni.$once('test', this.fn)
      },
      off() {
        uni.$off('test', this.fn)
62 63 64
      },
      offAll() {
        uni.$off('test')
H
hdx 已提交
65 66 67 68
      },
      emit() {
        uni.$emit('test', 'msg:' + Date.now())
      },
69 70 71
      emitWithObj() {
        uni.$emit('test-obj', { a: 1, b: 2 })
      },
H
hdx 已提交
72 73 74
      clear() {
        this.log.length = 0
      },
75 76 77 78 79 80 81 82 83 84 85 86 87
      testReturnId(){
        const id1 = uni.$on('test-return-id', this.fn)
        uni.$emit('test-return-id', '触发 test-return-id $on fn')
        uni.$off('test-return-id', id1)
        uni.$emit('test-return-id', '触发 test-return-id $on fn')

        uni.$once('test-return-id', this.fn)
        uni.$emit('test-return-id', '触发 test-return-id $once fn')
        uni.$emit('test-return-id', '触发 test-return-id $once fn')
        const id2 = uni.$once('test-id', this.fn)
        uni.$off('test-return-id', id2)
        uni.$emit('test-return-id', '触发 test-return-id $once fn')
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
88
    },
H
hdx 已提交
89
  }
Q
qiang 已提交
90 91 92
</script>

<style>
H
hdx 已提交
93 94 95
  .box {
    padding: 10px;
  }
96
</style>