question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template Refs inside v-for not working with Composition API

See original GitHub issue

Environment

  • Operating System: Linux
  • Node Version: v16.14.0
  • Nuxt Version: 3.0.0-27484505.9f6eff7
  • Package Manager: npm@8.3.1
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Sharing the SFC code.

<template>
  <div>
    <div v-for="i in 3" :key="i" class="content" ref="contentRef">
      Lorem ipsum dolor sit amet consectetur adipisicing elit.
    </div>
  </div>
</template>

<script setup>
const contentRef = ref([]);

onMounted(() => {
  console.log("On Mounted Ref:");
  console.log(JSON.stringify(contentRef.value.map((cr) => cr)));
});
</script>


<style scoped>
.content {
  display: table;
  padding: 30px;
  height: 60px;
  border-bottom: 1px solid grey;
}
</style>

Describe the bug

I am trying to use Template Refs inside v-for loop. But the corresponding reference returns an empty array.

The same example runs correctly on Vue SFC Playground.

Additional context

No response

Logs

On Mounted Ref:
[]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

can't use template ref on component in vue 3 composition api
I ran into this issue today. The problem is that, when using the <script setup> pattern, none of the declared variables are returned....
Read more >
How to use $refs in Vue 3 with Composition API and Script ...
First, you would declare a variable, initializing it to null, then you need to assign that variable name as a “ref” attribute on...
Read more >
The Beginners Guide to Template Refs - LearnVue
In this tutorial, we'll be learning all about template refs: how to use them in both the Options API and Composition API, some...
Read more >
Template Refs - Vue.js
ref is a special attribute, similar to the key attribute discussed in the v-for chapter. It allows us to obtain a direct reference...
Read more >
Vue 3 Composition API: How to Omit `.value` in refs
In this article, we discuss how to omit in VueJS refs, creating APIs ... For this to work, you will need to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found