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 in v-for not working

See original GitHub issue

Version

3.2.31

Reproduction link

stackblitz.com

Steps to reproduce

Open the DevTools and look at the console.logs from onMounted. You now see three logs, each representing a different template Ref. Log one and three are a single template ref and an array filled with function refs. They are working fine as expected. The issue is that the second ref is not returning an array of elements from the v-for but stays empty as initialized.

What is expected?

Template Ref on the v-for should be a DOM NodeList or an Array of DOMElements

What is actually happening?

The initialized ref([]) stays empty

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
lidlancacommented, Mar 8, 2022

@LinusBorg ignore this

The Ref are not normalized to even hit the fix code path that was due to a : binding on the ref when I tested.

The PR does indeed fix this issue for dev mode!

A possible workaround for the current limitation is:

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

//escapse template unwrapping
const skipUnwrap = { el: ref([]) };
// for convenience
let el = computed(() => skipUnwrap.el.value);

onMounted(() => {
  console.log(el.value);
});
</script>

<template>
  <div>
    <div v-for="i in 'hello'" :key="i" :ref="skipUnwrap.el">{{ i }}</div>
    Total Refs: {{ el.length }}
  </div>
</template>

https://stackblitz.com/edit/vitejs-vite-jfeskg?file=src/App.vue

5reactions
calebwaldnercommented, Mar 25, 2022

Workaround works 👍🏼 Looking forward to bug fix. Tx

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js ref inside the v-for loop - Stack Overflow
When using refs with v-for, the component / DOM nodes are stored as an array directly to the variable name so you don't...
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 >
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 >
Understanding refs in Vue - LogRocket Blog
Verify your version by running node -v in your terminal/command ... On template inspection in your browser, $refs do not show up at...
Read more >
How to use $refs in Vue 3 with Composition API and Script ...
It is important to clarify that refs should not be used unless strictly ... Let's write two simple example, for the Composition API...
Read more >

github_iconTop Related Medium Post

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