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

224
Views
¿Cuál es la mejor manera de activar una emisión secundaria desde el componente principal?

En este momento, estoy pasando un trigger prop del componente principal al secundario, que activa la emit del elemento secundario al principal.

parent component :

 <form @submit.prevent="state.store=true" method="post" enctype="multipart/form-data"> <child-component :triggerEmit=state.store @emitSomething="getSomething()"/>

child component :

 const emit = defineEmits([ 'emitBody' ]) watchEffect(async () => { if (props.triggerEmit) { emit('emitSomething', value) } })

Esto se vuelve confuso rápidamente, si los componentes aumentan de tamaño y me preguntaba si hay una forma más sencilla de activar child emits del padre, ya que este parece ser un caso de uso común.

Editar :

Intentando activar el child method directamente desde el padre (no funciona).

child :

 const childMethod = () => { console.log('check') }

parent :

html:

 <child ref="childRef"/>

configuración del guión:

 const childRef = ref() childRef.value.childMethod()

La página arroja un error:

 Cannot read properties of undefined (reading 'childMethod')
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Según tengo entendido, desea acceder a múltiples métodos/propiedades de componentes secundarios desde el componente principal. En caso afirmativo, puede lograrlo creando una referencia y accediendo a los métodos.

En plantilla :

 <!-- parent.vue --> <template> <button @click="$refs.childComponentRef.childComponentMethod()">Click me</button> <child-component ref="childComponentRef" /> </template>

En guión :

Con Vue 2 :

 this.$refs.childComponentRef.childComponentMethod( );

Con Vue 3 Composición Api :

 setup( ) { const childComponentRef = ref( ); childComponentRef.value.childComponentMethod( ) return { childComponentRef } }
about 4 years ago · Juan Pablo Isaza Report

0

En este caso, el activador del elemento primario consulta efectivamente al elemento secundario los datos de su evento para que pueda llamar a getSomething() en él. El padre ya posee getSomething() , por lo que realmente solo necesita los datos del hijo.

Otra forma de obtener esos datos es usar v-model para rastrear los datos secundarios:

  1. En el componente secundario, implemente v-model para una propiedad (una cadena, por ejemplo) declarando una propiedad modelValue y emitiendo un evento 'update:modelValue' con el nuevo valor como datos del evento:
 <!-- ChildName.vue --> <script setup> defineProps({ modelValue: String }) defineEmits(['update:modelValue']) </script> <template> <label>Name <input type="text" :value="modelValue" @input="$emit('update:modelValue', $event.target.value)"> </label> </template>
  1. En el padre, agregue un objeto reactive , que contenga un campo para el v-model de cada hijo:
 <!-- ParentForm.vue --> <script setup> import { reactive } from 'vue' const formData = reactive({ name: '', age: 0, address: { city: '', state: '', }, }) </script> <template> <form> <child-name v-model="formData.name" /> <child-age v-model="formData.age" /> <child-address v-model:city="formData.address.city" v-model:state="formData.address.state" /> <button>Submit</button> </form> </template>
  1. Ahora, el padre puede llamar a getSomething() en cada campo al enviar el formulario:
 <!-- ParentForm.vue --> <script setup> import { toRaw } from 'vue' ⋮ const getSomething = field => { console.log('getting', field) } const submit = () => { Object.entries(toRaw(formData)).forEach(getSomething) } </script> <template> <form @submit.prevent="submit"> ⋮ </form> </template>

manifestación

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!