uts-event-bus.uvue 2.4 KB
Newer Older
X
xty 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <view>
      <button @click="JsOnUts">1. js监听uts消息</button>
      <button @click="emitFromUts">2. uts中触发监听</button>
       <button @click="offUts">取消uts消息监听</button>

      <button @click="UtsOnJS">1. uts监听js消息</button>
      <button @click="emitFormJS">2 .js中触发监听</button>
      <button @click="offJs">取消js消息监听</button>
      <button @click="clear">清空消息</button>

      <view class="box">
        <view>收到的消息:</view>
        <view>
          <view v-for="(item, index) in log" :key="index">{{ item }}</view>
        </view>
      </view>
    </view>
    <button @click="testAll">test all</button>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  import {
    onJsMessage,
    offJsMessage,
    emitUtsMessage,
    getMessageChannel,
    getRevJsMessage,
    clearJsMessage,
    onJsMessageOnce,
  } from "@/uni_modules/uts-eventbus"


  export default {
    data() {
      return {
        log: [] as string[],
      }
    },
    methods: {
      fn(res : string, res2 : string) {
        console.log("on rev: " + res)
        this.log.push(res)
        this.log.push(res2)
      },
      fn2(res : string) {
        this.log.push(res)
      },
      JsOnUts() {
        uni.$off(getMessageChannel(), this.fn)
        uni.$on(getMessageChannel(), this.fn)
      },
      offUts() {
        uni.$off(getMessageChannel(), this.fn)
      },
      emitFromUts() {
        emitUtsMessage("emit form uts")
      },
      JsOnUtsOnce() {
        uni.$once(getMessageChannel(), this.fn2)
      },
      UtsOnJS() {
        onJsMessage("JsMessage")
      },
      UtsOnJSOnce() {
        onJsMessageOnce("JsMessage")
      },
      offJs() {
        offJsMessage("JsMessage")
      },
      emitFormJS() {
        clearJsMessage()
        uni.$emit("JsMessage", "emit form js")
        let msg = getRevJsMessage()
        console.log("message:"+msg)
        if (msg && msg.length){
          this.log.push(msg)
        }
      },
      clear() {
        clearJsMessage()
        this.log.length = 0
      },
      testAll() {
      	this.JsOnUts();
      	this.emitFromUts();
      	this.UtsOnJS();
      	this.emitFormJS();
      }
    },
  }
</script>

<style>
  .box {
    padding: 10px;
  }
</style>