webfansplz / vuejs-challenges

Collection of Vue.js challenges
https://vuejs-challenges.netlify.app/
MIT License
2.71k stars 187 forks source link

10 - Lifecycle Hooks #2820

Open eljefe213 opened 6 days ago

eljefe213 commented 6 days ago
<script setup lang="ts">
import { onMounted, onUnmounted, inject } from "vue"

const timer = inject('timer' )
const count = inject('count')

onMounted(() => {

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

})

onUnmounted(() => {

    window.clearInterval(timer.value)

})

</script>

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