event-bus.uvue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view class="box">
  6. <button class="uni-btn" @click="on">开始监听</button>
  7. <button class="uni-btn" @click="once">监听一次</button>
  8. <button class="uni-btn" @click="off">取消监听</button>
  9. <!-- <button @click="offAll">取消全部监听</button> -->
  10. <button class="uni-btn" @click="emit">触发监听</button>
  11. <button class="uni-btn" @click="clear">清空消息</button>
  12. <view>
  13. <view class="uni-btn">收到的消息:</view>
  14. <view class="uni-btn">
  15. <view v-for="(item, index) in log" :key="index">{{ item }}</view>
  16. </view>
  17. <button class="uni-btn" @click="onObj">开始监听 obj 参数</button>
  18. <button class="uni-btn" @click="emitWithObj">触发监听 obj 参数</button>
  19. <view class='uni-btn'>
  20. <text>接收到的 obj 参数:</text>
  21. <text>{{ JSON.stringify(objArg) }}</text>
  22. </view>
  23. <button class='uni-btn' @click="testReturnId">测试返回 id</button>
  24. <button class='uni-btn' @click="testEmitNoArgs">测试 $emit 无参</button>
  25. <button class='uni-btn' @click="testEmitMultipleArgs">测试 $emit 多个参数</button>
  26. </view>
  27. </view>
  28. <!-- #ifdef APP -->
  29. </scroll-view>
  30. <!-- #endif -->
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. log: [] as string[],
  37. objArg: {},
  38. }
  39. },
  40. methods: {
  41. fn(res : string) {
  42. this.log.push(res)
  43. },
  44. fn2(res : string) {
  45. this.log.push(res)
  46. },
  47. on() {
  48. uni.$on('test', this.fn)
  49. },
  50. on2() {
  51. uni.$on('test', this.fn2)
  52. },
  53. onObj() {
  54. uni.$on('test-obj', (res : UTSJSONObject) => {
  55. this.objArg = res
  56. })
  57. },
  58. once() {
  59. uni.$once('test', this.fn)
  60. },
  61. off() {
  62. uni.$off('test', this.fn)
  63. },
  64. offAll() {
  65. uni.$off('test')
  66. },
  67. emit() {
  68. uni.$emit('test', 'msg:' + Date.now())
  69. },
  70. emitWithObj() {
  71. uni.$emit('test-obj', { a: 1, b: 2 })
  72. },
  73. clear() {
  74. this.log.length = 0
  75. },
  76. testReturnId(){
  77. const id1 = uni.$on('test-return-id', this.fn)
  78. uni.$emit('test-return-id', '触发 test-return-id $on fn')
  79. uni.$off('test-return-id', id1)
  80. uni.$emit('test-return-id', '触发 test-return-id $on fn')
  81. uni.$once('test-return-id', this.fn)
  82. uni.$emit('test-return-id', '触发 test-return-id $once fn')
  83. uni.$emit('test-return-id', '触发 test-return-id $once fn')
  84. const id2 = uni.$once('test-id', this.fn)
  85. uni.$off('test-return-id', id2)
  86. uni.$emit('test-return-id', '触发 test-return-id $once fn')
  87. },
  88. testEmitNoArgs() {
  89. uni.$on('test-emit-no-args', () => {
  90. this.log.push('test-emit-no-args')
  91. })
  92. uni.$emit('test-emit-no-args')
  93. uni.$off('test-emit-no-args')
  94. },
  95. testEmitMultipleArgs() {
  96. uni.$on('test-emit-multiple-args', (arg1 : string, arg2 : number) => {
  97. this.log.push(`${arg1}_${arg2}`)
  98. })
  99. uni.$emit('test-emit-multiple-args', 'arg1', 2)
  100. uni.$off('test-emit-multiple-args')
  101. }
  102. },
  103. }
  104. </script>
  105. <style>
  106. .box {
  107. padding: 10px;
  108. }
  109. </style>