theme-change.uvue 1.8 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2
<template>
  <view class="uni-padding-wrap">
3
    <view class="uni-common-mt item-box">
DCloud-WZF's avatar
DCloud-WZF 已提交
4 5 6
      <text>theme:</text>
      <text id="theme">{{ dataInfo.theme }}</text>
    </view>
7
    <view class="uni-common-mt item-box">
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9 10
      <text>fn1 trigger num:</text>
      <text id="fn1-trigger-num">{{ dataInfo.fn1TriggerNum }}</text>
    </view>
11
    <view class="uni-common-mt item-box">
DCloud-WZF's avatar
DCloud-WZF 已提交
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
      <text>fn2 trigger num:</text>
      <text id="fn2-trigger-num">{{ dataInfo.fn2TriggerNum }}</text>
    </view>
    <button class="uni-common-mt" id="bind-fn1" @click="bindFn1">
      bind fn1
    </button>
    <button class="uni-common-mt" id="remove-fn1" @click="removeFn1">
      remove fn1
    </button>
    <button class="uni-common-mt" id="bind-fn2" @click="bindFn2">
      bind fn2
    </button>
    <button class="uni-common-mt" id="remove-fn2" @click="removeFn2">
      remove fn2
    </button>
  </view>
</template>

<script setup lang="uts">
type DataInfo = {
  fn1TriggerNum: number
  fn2TriggerNum: number
  theme: string

}

const dataInfo = reactive<DataInfo>({
  fn1TriggerNum: 0,
  fn2TriggerNum: 0,
  theme: ''
})

const fn1 = (options : OnThemeChangeOptions) => {
  dataInfo.fn1TriggerNum++
  console.log('fn1 triggered options: ', options)
  dataInfo.theme = options.theme
}

const fn2 = (options : OnThemeChangeOptions) => {
  dataInfo.fn2TriggerNum++
  console.log('fn2 triggered options: ', options)
  dataInfo.theme = options.theme
}

const bindFn1 = () => {
  uni.onOSThemeChange(fn1)
}
const removeFn1 = () => {
  uni.offOSThemeChange(fn1)
}
const bindFn2 = () => {
  uni.onOSThemeChange(fn2)
}
const removeFn2 = () => {
  uni.offOSThemeChange(fn2)
}

onReady(() => {
  bindFn1()
  bindFn2()
})
</script>

<style>
76
.item-box {
DCloud-WZF's avatar
DCloud-WZF 已提交
77 78 79 80 81
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>