Skip to content
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

ref doesn't work properly with v-for together #1166

Closed
Jinjiang opened this issue May 11, 2020 · 8 comments
Closed

ref doesn't work properly with v-for together #1166

Jinjiang opened this issue May 11, 2020 · 8 comments
Labels
has workaround A workaround has been found to avoid the problem

Comments

@Jinjiang
Copy link
Member

Version

3.0.0-beta.10

Reproduction link

https://codesandbox.io/s/goofy-yalow-d0x6e?file=/src/App.vue

Steps to reproduce

<template>
  <span v-for="n in 10" ref="x">{{ n }}</span>
</template>

<script>
export default {
  mounted() {
    console.log(this.$refs.x);
  }
}
</script>

What is expected?

print an array of all spans

What is actually happening?

the last span


https://vuejs.org/v2/api/#ref

When used on elements/components with v-for, the registered reference will be an Array containing DOM nodes or component instances.

@yyx990803
Copy link
Member

Vue 3 no longer has special behavior for v-for refs: https://composition-api.vuejs.org/api.html#template-refs

@yyx990803 yyx990803 reopened this May 11, 2020
@yyx990803
Copy link
Member

yyx990803 commented May 11, 2020

Actually, we may need to detect non composition api usage for backwards compat. Or we need an RFC to officially deprecate this (I never considered it good practice anyway, and it creates a TON of complexity when used inside nested v-fors.)

@Jinjiang
Copy link
Member Author

Gotcha. My quick idea is introducing another refs="x" for v-for which equals to :ref="el => $refs.x[i] = el. So it’s easy to code-mod and deprecate further I guess. Maybe will propose something like that later to RFCs. Thanks.

@sagalbot
Copy link

It's worth noting that the current v3 docs do explicitly note support for v-for with refs:

When ref is used together with v-for, the ref you get will be an array containing the child components mirroring the data source.

https://v3.vuejs.org/guide/component-template-refs.html

If this should be changed to reflect the current status I'm happy to PR.

@syuilo
Copy link
Contributor

syuilo commented Sep 12, 2020

Is there any migration strategy for about this?

@RobbinBaauw RobbinBaauw mentioned this issue Sep 17, 2020
@LinusBorg LinusBorg added the ✨ feature request New feature or request label Dec 17, 2020
@LinusBorg
Copy link
Member

Actually, we may need to detect non composition api usage for backwards compat. Or we need an RFC to officially deprecate this (I never considered it good practice anyway, and it creates a TON of complexity when used inside nested v-fors.)

@yyx990803 We actually can do the same thing we recommend for the composition with the options API:

Example: https://jsfiddle.net/Linusborg/xjcbyw2f/1/

This should in theory also allow people to come up with a strategy for nested v-for situations (which, I agree, are a complexity nightmare)

@syuilo That should work as a migration strategy I think.

@vuejs/docs Can we add this example to the migration guide for Options-API users?

@LinusBorg LinusBorg added has workaround A workaround has been found to avoid the problem and removed ✨ feature request New feature or request labels Dec 17, 2020
@nichoquet
Copy link

Im not sure to understand why you say it can create complexity. Isn't the goal of this feature to be an alternative to document.getElementById and document.getElementByClassName without breaking the component logic (document.getElementById can access DOM on other components but ref cant)? Isn't :ref="el => listElements.push(el)" less readable then :ref=listElements" ?
I just want to understand why this decision happened, because I just can't see a good reason why it should have

@LinusBorg
Copy link
Member

We actually changed our minds about his and reintroduced the old behavior quite recently.

Currently there still is a bug (#5447) but we will patch that ver soon.

So I'll close this old issue about this.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem
Projects
None yet
Development

No branches or pull requests

6 participants