Subscribe on changes!

Vue 3 get parent component

avatar
Aug 28th 2021

Version

3.2.6

Reproduction link

jsfiddle.net/posva/km2qpwx7/

Steps to reproduce

In Vue 2, we can get parent element like this: this.$vnode.elm.parentElement

What is expected?

It gives parent element

What is actually happening?

TypeError: Cannot read property 'elm' of undefined


What does $vnode equal in Vue 2

avatar
Aug 28th 2021

$vnode was always internal thing, it's better to just use refs to grab DOM element

<script setup>
  import { ref, onMounted } from 'vue'
  
  const root = ref(null)

  onMounted(() => {
    console.log(root.value.parentElement)
  })
</script>

<template>
  <div ref="root">
    
  </div>
</template>
avatar
Aug 28th 2021

I thank you so much @sqal.

avatar
Aug 29th 2021

@edison1105 why? vue docs recommends using ref

avatar
Aug 29th 2021

@edison1105 why? vue docs recommends using ref

Oh sorry. I got it wrong.