uts-event-bus.uvue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <button @click="JsOnUts">1. js监听uts消息</button>
  7. <button @click="emitFromUts">2. uts中触发监听</button>
  8. <button @click="emitUtsMessageUTSObject">2.1 uts中触发监听(UTSJSONObject)</button>
  9. <button @click="emitUtsMessages">2.2 uts中触发监听(多个参数)</button>
  10. <button @click="emitUtsMessageNoArgument">2.3 uts中触发监听(无参数)</button>
  11. <button @click="offUts">取消uts消息监听</button>
  12. <button @click="UtsOnJS">1. uts监听js消息</button>
  13. <button @click="emitFormJS">2 .js中触发监听</button>
  14. <button @click="emitFormJSObject">2.1 js中触发监听(UTSJSONObject)</button>
  15. <button @click="offJs">取消js消息监听</button>
  16. <button @click="clear">清空消息</button>
  17. <view class="box">
  18. <view>收到的消息:</view>
  19. <view>
  20. <view v-for="(item, index) in log" :key="index">{{ item }}</view>
  21. </view>
  22. </view>
  23. </view>
  24. <button @click="testAll">test all</button>
  25. <!-- #ifdef APP -->
  26. </scroll-view>
  27. <!-- #endif -->
  28. </template>
  29. <script>
  30. import {
  31. onJsMessage,
  32. offJsMessage,
  33. emitUtsMessage,
  34. emitUtsMessageUTSObject,
  35. emitUtsMessageNoArgument,
  36. emitUtsMessages,
  37. getMessageChannel,
  38. getRevJsMessage,
  39. clearJsMessage,
  40. onJsMessageOnce,
  41. } from "@/uni_modules/uts-eventbus"
  42. export default {
  43. data() {
  44. return {
  45. log: [] as string[],
  46. }
  47. },
  48. methods: {
  49. fn(res : any, res2 : any) {
  50. if (res != null){
  51. console.log("on rev: " + JSON.stringify(res))
  52. this.log.push(res)
  53. }
  54. if (res2 != null) {
  55. console.log("on rev: " + JSON.stringify(res2))
  56. this.log.push(res2)
  57. }
  58. },
  59. fn2(res : string) {
  60. this.log.push(res)
  61. },
  62. JsOnUts() {
  63. uni.$off(getMessageChannel(), this.fn)
  64. uni.$on(getMessageChannel(), this.fn)
  65. },
  66. offUts() {
  67. uni.$off(getMessageChannel(), this.fn)
  68. },
  69. emitFromUts() {
  70. emitUtsMessage("emit form uts")
  71. },
  72. emitUtsMessageUTSObject() {
  73. emitUtsMessageUTSObject({
  74. latitude: 39.951028,
  75. longitude: 116.354662,
  76. name: '金运大厦',
  77. address: '西直门北大街xx号'
  78. })
  79. },
  80. emitUtsMessages() {
  81. emitUtsMessages({
  82. latitude: 39.951028,
  83. longitude: 116.354662,
  84. name: '金运大厦',
  85. address: '西直门北大街xx号'
  86. }, "emit form uts")
  87. },
  88. emitUtsMessageNoArgument() {
  89. emitUtsMessageNoArgument()
  90. },
  91. JsOnUtsOnce() {
  92. uni.$once(getMessageChannel(), this.fn2)
  93. },
  94. UtsOnJS() {
  95. onJsMessage("JsMessage")
  96. },
  97. UtsOnJSOnce() {
  98. onJsMessageOnce("JsMessage")
  99. },
  100. offJs() {
  101. offJsMessage("JsMessage")
  102. },
  103. emitFormJS() {
  104. clearJsMessage()
  105. uni.$emit("JsMessage", "emit form js")
  106. let msg = getRevJsMessage()
  107. console.log("message:"+msg)
  108. if (msg && msg.length){
  109. this.log.push(msg)
  110. }
  111. },
  112. emitFormJSObject() {
  113. clearJsMessage()
  114. uni.$emit("JsMessage", {
  115. latitude: 39.951028,
  116. longitude: 116.354662,
  117. name: '金运大厦form js',
  118. address: '西直门北大街xx号 from js'
  119. })
  120. let msg = getRevJsMessage()
  121. console.log("message:"+msg)
  122. if (msg && msg.length){
  123. this.log.push(msg)
  124. }
  125. },
  126. clear() {
  127. clearJsMessage()
  128. this.log.length = 0
  129. },
  130. testAll() {
  131. this.JsOnUts();
  132. this.emitFromUts();
  133. this.UtsOnJS();
  134. this.emitFormJS();
  135. }
  136. },
  137. }
  138. </script>
  139. <style>
  140. .box {
  141. padding: 10px;
  142. }
  143. </style>