Skip to content

10 - Lifecycle Hooks #2959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Rockchtaa opened this issue Apr 10, 2025 · 0 comments
Open

10 - Lifecycle Hooks #2959

Rockchtaa opened this issue Apr 10, 2025 · 0 comments

Comments

@Rockchtaa
Copy link

Rockchtaa commented Apr 10, 2025

//child

<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(() => {
  clearInterval(timer.value)
  timer.value = null
})
</script>

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

<script setup lang="ts">
import { ref, provide } from "vue"
import Child from "./Child.vue"

const visible = ref(true)
const timer = ref(null)
const count = ref(0)
provide("timer", timer)
provide("count", count)

function toggle() {
  visible.value = !visible.value
}
</script>

<template>
  <div>
    <Child v-if="visible" />
    <p>
      <button @click="toggle">
        Toggle Child Component
      </button>
    </p>
  </div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant