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

205
Views
this.$emit() no es una función al intentar cambiar el estado en el componente principal del elemento secundario en la API de composición en vue 3

//Componente padre

 <template> <childComp @onchangeData='changeData' /> </template> <script> setup() { const state = reactive({ data: 'anything }); function changeData(v){ state.data = v } return { changeData} }, </script>

//Niño

 <template> <button @click='change('hello')' /> </template> <script> setup() { function change(v){ this.$emit('onchangeData', v) } return{change} }, </script>

Estoy luchando por cambiar el estado reactivo de los padres al hacer clic en el botón del niño. Está diciendo esto. $ emit no es una función . Intenté muchas formas, como usar @onchangeData='changeData()' en lugar de @onchangeData='changeData' , usar funciones de flecha, etc. Pero nada funciona. Aquí, escribí un ejemplo y un código mínimo para mantenerlo simple. Pero espero que mi problema sea claro.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Mire el siguiente fragmento, this es lo mismo en la composición que en la API de opciones, por lo que debe usar la función emit pass to setup:

 const { reactive } = Vue const app = Vue.createApp({ setup() { const state = reactive({ data: 'anything' }); function changeData(v){ state.data = v } return { changeData, state } }, }) app.component("ChildComp", { template: ` <div> <button @click="change('hello')">click</button> </div> `, setup(props, {emit}) { function change(v){ emit('onchangeData', v) } return { change } }, }) app.mount('#demo')
 <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script> <div id="demo"> <child-comp @onchange-data='changeData'></child-comp> <p>{{ state.data }}</p> </div>

about 4 years ago · Juan Pablo Isaza 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!