Skip to content

10 - 生命周期钩子 #2287

Open
Open
@wxh233

Description

@wxh233
// 你的答案
child.vue文件添加onBeforeUnmount 钩子

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

const timer = inject("timer")
const count = inject("count")

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

onBeforeUnmount(()=>{
  timer.value && clearInterval(timer.value)
})

</script>

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions