Skip to content

How to get ref of an element behind v-if #6127

Discussion options

You must be logged in to vote

You can use like this Example, Hope this helps you.

<script setup>
import { onMounted, ref } from "vue";

const div$ = ref();
const flag$ = ref(false);

onMounted(() => {
  setTimeout(() => {
    flag$.value = true;
  }, 1000);

  setTimeout(() => {
    console.log(`ref: `, div$.value); // this will be undefined
  }, 2000);
});
</script>

<template>
  <div>
    <div ref="div$" v-if="flag$">Hello</div>
  </div>
</template>
new.mp4

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by CGQAQ
Comment options

You must be logged in to vote
1 reply
@chenxch
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants