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

Refs incorrect when using v-for and combining components and elements #5997

Closed
floorish opened this issue Jun 29, 2017 · 3 comments · Fixed by #6039
Closed

Refs incorrect when using v-for and combining components and elements #5997

floorish opened this issue Jun 29, 2017 · 3 comments · Fixed by #6039

Comments

@floorish
Copy link

Version

2.3.4

Reproduction link

https://jsfiddle.net/floorish/7td2offb/

Steps to reproduce

  • Parent with dynamic components, referenced by ref: <component :is="child.type" v-for="child in children" :key="child.id" ref="children"></component>
  • Multiple child.types where one of them uses a loading component: <loading v-if="isLoading"></loading> <div v-else>Child B ready</div>
  • In Child switch isLoading from true to false after a timeout/async call

What is expected?

parent.$refs.children equals the displayed child components

What is actually happening?

parent.$refs.children only includes the child component that has the loading mechanism


This is very much an edge case, but it took me a while to figure out any solutions. Most trivial solution is to wrap the Child template in another <div>, but AFAIK the <div v-if="val"></div> <div v-else></div> construct in the root of a template is supported in Vue (there is always a single root element).

Other solutions are provided in reproduction link.

@posva posva changed the title Refs incorrect when using v-for and "loading" children Refs incorrect when using v-for and combining components and elements Jun 29, 2017
@no1xsyzy
Copy link

no1xsyzy commented Jul 6, 2017

Cited from https://vuejs.org/v2/guide/components.html#Child-Component-Refs

$refs are only populated after the component has been rendered, and it is not reactive.

I think you should not use it for dynamical referring to components. try to use props / $emit.

@floorish
Copy link
Author

floorish commented Jul 6, 2017

That might have something to do with it. But in this case the $refs does change after it has been populated initially, which (for me) is unexpected.

@no1xsyzy
Copy link

no1xsyzy commented Jul 6, 2017

You actually can call any function to generate a string for the ref attribute (with full reactive support).

Although somehow difficult, it is even possible to change that from the child component (add new data to parent and $emit an event from child to tell the parent to change ref ) - although it may pollute the code.

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

Successfully merging a pull request may close this issue.

2 participants