Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Manejador de observación API de composición

¿Cómo refactorizo esto?

 watch: { count: { handler (v) { if (v) this.intervalId = setInterval(() => { this.now++ }, 1000) else clearInterval(this.intervalId) }, immediate: true } }

¿En la API de composición?



Intenté algo así, que arroja errores:

 watch(()=> { count.value, handler (value) { if (value) intervalId.value = setInterval(() => { now.value++; }, 1000); else clearInterval(intervalId.value); }, immediate: true })
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede refactorizarlo como se muestra a continuación. Puede pasar la opción immediate al método de watch como tercer parámetro en Vue 3

 const app = Vue.createApp({ setup() { const count = Vue.ref(0); const now = Vue.ref(0); const intervalId = Vue.ref(0); Vue.watch(count, (currentValue, oldValue) => { if (currentValue) intervalId.value = setInterval(() => now.value++, 1000); }, { immediate: true }); Vue.onUnmounted(() => { clearInterval(intervalId.value) }); return { count, now }; } }).mount('#app');
 <script src="https://unpkg.com/vue@next"></script> <div id="app"> {{ count }} - {{ now }} <button @click="count++">Increment</button> <button @click="count=0">reset</button> </div>

Referencia:
  • API de composición: ganchos de ciclo de vida

PD: puede usar import {watch, ref, onUnmounted} from "vue" para hacer que el código sea más limpio.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!