event-bus.uvue 3.3 KB
Newer Older
Q
qiang 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2
  <!-- #ifdef APP -->
H
hdx 已提交
3
  <scroll-view class="page-scroll-view">
4 5 6 7 8
    <!-- #endif -->
    <view class="box">
      <button class="uni-btn" @click="on">开始监听</button>
      <button class="uni-btn" @click="once">监听一次</button>
      <button class="uni-btn" @click="off">取消监听</button>
9
      <!-- <button @click="offAll">取消全部监听</button> -->
10 11 12 13 14
      <button class="uni-btn" @click="emit">触发监听</button>
      <button class="uni-btn" @click="clear">清空消息</button>
      <view>
        <view class="uni-btn">收到的消息:</view>
        <view class="uni-btn">
DCloud-WZF's avatar
DCloud-WZF 已提交
15 16
          <view v-for="(item, index) in log" :key="index">{{ item }}</view>
        </view>
17 18 19
        <button class="uni-btn" @click="onObj">开始监听 obj 参数</button>
        <button class="uni-btn" @click="emitWithObj">触发监听 obj 参数</button>
        <view class='uni-btn'>
DCloud-WZF's avatar
DCloud-WZF 已提交
20
          <text>接收到的 obj 参数:</text>
21
          <text>{{ JSON.stringify(objArg) }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
22
        </view>
23 24 25
        <button class='uni-btn' @click="testReturnId">测试返回 id</button>
        <button class='uni-btn' @click="testEmitNoArgs">测试 $emit 无参</button>
        <button class='uni-btn' @click="testEmitMultipleArgs">测试 $emit 多个参数</button>
Q
qiang 已提交
26 27
      </view>
    </view>
28
    <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
29 30
  </scroll-view>
  <!-- #endif -->
Q
qiang 已提交
31 32
</template>

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

83 84 85 86 87 88 89 90 91 92 93 94 95
      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')
    },
    testEmitNoArgs() {
      uni.$on('test-emit-no-args', () => {
        this.log.push('test-emit-no-args')
      })
      uni.$emit('test-emit-no-args')
      uni.$off('test-emit-no-args')
DCloud-WZF's avatar
DCloud-WZF 已提交
96
    },
97 98 99 100 101 102 103 104 105
    testEmitMultipleArgs() {
      uni.$on('test-emit-multiple-args', (arg1 : string, arg2 : number) => {
        this.log.push(`${arg1}_${arg2}`)
      })
      uni.$emit('test-emit-multiple-args', 'arg1', 2)
      uni.$off('test-emit-multiple-args')
    }
  },
}
Q
qiang 已提交
106 107 108
</script>

<style>
109 110 111
.box {
  padding: 10px;
}
112
</style>