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

227
Visualizações
Can't bind html elements to refs in other files

so I have this weird problem, I'm trying to bind a HTML element to my ref in my store.ts file. In script setup the ref logs null, but when I use the regular setup(){} it does work. I think my ref isn't getting exposed to the template. What seems to be the issue?

app.vue

<template>
  <div ref="elementRef">
  <p>Child of ref</p>
  </div>
  <button @click="doSomeShit">Test</button>
</template>
<script setup lang="ts">
import {ref, defineComponent} from 'vue'
import {elementRef} from './store/store'

    const doSomeShit = () => {
      console.log(elementRef.value)
    }
</script>
store.ts

import {ref} from 'vue'

export const elementRef = ref<Maybe<HTMLElement>>(null)

interface Maybe<T> {
    T: T | null
}

But this does work

<template>
  <div ref="elementRef"  class="h-48 ">
  <p>Child of ref</p>
  </div>
  <button @click="doSomeShit">Test</button>
</template>


<script lang="ts">
import {ref, defineComponent} from 'vue'
import {elementRef} from './store/store'

export default defineComponent({
  setup(){
    const doSomeShit = () => {
      console.log(elementRef.value)
    }


  return{
    doSomeShit,
    elementRef
  }
  }
})
</script>
about 4 years ago · Juan Pablo Isaza
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