Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
vuejs how to access a ref inside a child component?

I am new to vuejs and not very experienced in JavaScript. I use Vue3 in Laravel

I have a child component which exposes a ref on an input like this

<input 
    v-model="raw_input" 
    ref="raw"  
    @input="checkLength(limit)" 
    @keydown="enterNumbers($event)" 
    type="number" 
/>

const raw = ref('');
defineExpose({
    raw
})

In the parent

<template lang="">
    <PageComponent title="Dashboard">
    <SmartVolumeInputVue ref="svi" />
    <div class="mt-16 border  h-8">
    {{ val }}
    </div>
    </PageComponent>
</template>

<script setup>
import PageComponent from "../components/PageComponent.vue"
import SmartVolumeInputVue from "../components/customInputs/SmartVolumeInput.vue";import { computed, ref } from "vue";

const svi = ref(null)
//let val=svi
//let val=svi.raw
let val=svi.raw.value
</script>

At the bottom of the script there are 3 lines (I only uncomment one at a time) The first displays this in the template

{ "raw": "[object HTMLInputElement]" }

The second displays nothing

And the third reports an error

Uncaught (in promise) TypeError: svi.raw is undefined

I need some help to get the value of the referenced input in the child component.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Do you need the ref on the element? Or only the value?

If not, just expose the value like this: Live example

// App.vue
<script setup>
import { ref } from 'vue'
import SmartVolumeInputVue from './SmartVolumeInput.vue'
const svi = ref()
</script>

<template>
    <SmartVolumeInputVue ref="svi" />
  <div>
    {{svi?.raw_input}}
  </div>
</template>
// SmartVolumeInput.vue
<script setup>
import { ref, defineExpose } from 'vue'
const raw_input = ref(123)
defineExpose({
  raw_input
})
</script>

<template>
  <input v-model.number="raw_input" type="number" />
</template>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda