-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template Refs in v-for not working #5525
Comments
this works in playground, and "prod" build |
I have the same problem too, and i can give another repro case here, the |
duplicate of #5447 |
Have you solved this problem? How to solve it |
Expect to get feedback on this problem, in the process of writing business code, stuck in this piece |
@LinusBorg In that case this issue does share a common root cause. However i am not sure my pr covers the dev build case. The Ref are not normalized to even hit the fix code path |
I'd have to take a closer look, but in dev, the ref would be part of |
@LinusBorg ignore this
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 |
Same problem :( , hope this bug is fixed soon. |
@lidlanca Thank you very much for your method, as far as I am concerned, there is no problem now. good good |
Workaround works 👍🏼 Looking forward to bug fix. Tx |
Same issue here, thank you for the workaround waiting for the fix. 👍 |
I'll add my name to the list of people affected by this. I am converting a mid-sized app to the composition API and this breaks some core functionality. I'll try the workaround, but it seems shaky. |
it's still works only with wrapper... var itemRefs = ref( [] )
var skipUnwrap = { itemRefs }
div(
v-for=" i in ls " :ref=" skipUnwrap.itemRefs "
) |
same problem. Still only works with wrapper |
not working for me as well |
@Sven89 @vervelover as a workaround. |
Hi, thank you for the answer, unfortunately all workarounds posted seem to add values to the ref array, but contrary to what I was able to do in vue 2, in vue 3 I cannot access any method defined on the ref. So let's say I have a
But how do I call the method in vue 3 |
Use |
That was awesome, thank you! I still do not understand why this bug is closed though, because I still get an empty array of refs when not using the object wrap workaround.. |
After I updated with the version that fixed it, I was able to remove the workaround and ref worked properly again. Unless there was another regression, I can attest to the bug having been fixed. |
@vervelover |
I experienced this issue yesterday in the newest Vue version. Only in Vite build not in Vite dev. With the wrapper it worked in both. So this issue is still relevant? |
Can you provide a minimum reproduction? Thanks. |
This is a simplified example: Works fine in dev but not once it is builded. |
I have download stackblitz.com/edit/vitejs-vite-bqescg?file=src/App.vue to local and build use Vite, but it will also work fine. |
@liulinboyi any special configs options used there? |
The repository is here. |
@liulinboyi gonna test this next week, thanks! |
@liulinboyi I cant explain why but it is working now (after a node_modules delete and reinstall) haha. Thanks anyway! |
Also spent a bit of time on this, the way it works for me: <script setup>
const items = ['i1', 'i2', 'i3'];
const elRefs = reactive([]);
function updateRefs(el, index) {
elRefs[index] = el;
}
</script>
<template>
<div>
<div v-for="item, index in items" :key="index"
:ref="(el) => getPlayerRef(el, index)">
{{ item }}
</div>
</div>
</template> Also works with components and exposing function on these components. |
Hey @Seanitzel Where do you get the index inside your updateRefs from? |
@madsh93 Oops my bad, messed up the rewrite from my code to the comment lol Edited my comment, should be clear now :) |
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 DOMElementsWhat is actually happening?
The initialized
ref([])
stays emptyThe text was updated successfully, but these errors were encountered: