vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

10 - 生命周期钩子

Open zhouLion opened this issue 11 months ago • 0 comments

<script setup lang="ts">
import { onMounted, inject, onUnmounted } from "vue"
import type { Ref } from "vue"

const timer = inject<Ref<ReturnType<typeof window.setInterval>>>("timer")
const count = inject<Ref<ReturnType<typeof window.setInterval>>>("count")

onMounted(() => {
  timer.value = window.setInterval(() => {
    count.value++
  }, 1000)
})

onUnmounted(() => {
  window.clearInterval(timer.value)
})

</script>

<template>
  <div>
    <p>
      Child Component: {{ count }}
    </p>
  </div>
</template>

zhouLion avatar Mar 22 '24 17:03 zhouLion