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

136
Visualizações
this.$refs returning undefined in a Nuxt application (trying to read the height of a div)

I want to give the same height to a div containing Component2 as another div (modelDiv) containing Component1.

<template>
  <div>
    <div v-if="showMe">
      <div ref="modelDiv">
        <Component1/>
      </div>
    </div>
    <div v-else>
      <div :style="setDivHeight">
        <Component2/>
      </div>
    </div>
  </div>
</template>

<script>

export default {

  data() {
    return {
      displayMe: true,
      elementHeight: null,
    }
  },
  computed: {
    showMe() {
      return this.displayMe
    },
    setDivHeight() {
      return `height: ${this.elementHeight}px;`
    },
  },

  mounted() {
    this.$nextTick(function () {
      this.elementHeight = this.$refs.modelDiv[0].$el.clientHeight
    })
  },
}
</script>

I am having this error message TypeError: Cannot read properties of undefined (reading '$el') and console.log('the height', this.$refs) gives me modelDiv: undefined. I can't figure out why. Any help is welcome.

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

0

That should work:

this.elementHeight = this.$refs.modelDiv.clientHeight;

Demo: https://codesandbox.io/s/ref-kloqu?file=/src/App.vue

about 4 years ago · Juan Pablo Isaza Relatório

0

Since I see only see one div with ref as modelDiv, jus try using one of the below options amd see if that works

this.elementHeight = this.$refs.modelDiv.$el.clientHeight

Or

this.elementHeight = this.$refs.modelDiv.clientHeight
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