on-scope-dispose.uvue 821 字节
Newer Older
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
<template>
  <view class="page">
    <text id="has-current-scope">hasCurrentScope: {{ hasCurrentScope }}</text>
    <button id="create-scope-btn" class="uni-common-mt" @click="createScope">create scope</button>
    <button id="stop-scope-btn" class="uni-common-mt" @click="stopScope">stop scope</button>
  </view>
</template>

<script setup>
  const hasCurrentScope = ref(false)

  let scope = null as EffectScope | null

  const createScope = () => {
    scope = effectScope();
    (scope as EffectScope).run(() => {
      hasCurrentScope.value = getCurrentScope() !== null
      onScopeDispose(() => {
        hasCurrentScope.value = getCurrentScope() !== null
      })
    })
  }

  const stopScope = () => {
    if (scope !== null) {
      (scope as EffectScope).stop()
    }
  }
</script>