-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
Console warning when using a v-if on an element inside a v-for on a template #1734
Comments
Another problem with same root cause, now using I suppose a solution for some cases is using But nesting |
I tried wrapping the Instead of having : <template v-for="({id, text}, index) in items">
<span :key="'text' + id">{{text}}</span>
<span v-if="index < numberOfItems - 1" :key="'sep' + id">, </span>
</template> We have to write it like this : <template v-for="({id, text}, index) in items">
<span :key="'text' + id">{{text}}</span>
<template v-if="index < numberOfItems - 1">
<span :key="'sep' + id">, </span>
</template>
</template> I found after posting this comment : #1712 (comment), but I'm not sure if I understand why this would cause this use case not to be valid. |
This is expected - in v3 |
I see, thanks for the explanation ! It sure is more readable than having to wrap the v-if on the child, and it works just fine. Can we expect to have the ESlint default config to be updated to handle this syntax, though ? (I have the
|
So we need to update the |
@sodatea @yyx990803 I have something I don't understand, so please let me know if you know. <div>
<!-- Case1 -- The key is attached to _Fragment.-->
<template v-for="item in list" :key="item.id1">
<MyButton :foo="item">One</MyButton>
<MyButton :foo="item">Two</MyButton>
</template>
<!-- Case2 -- The key is not attached to _Fragment. -->
<template v-for="item in list" :key="item.id2">
<MyButton :foo="item">One</MyButton>
</template>
<!-- Case3 -- The key is attached to div. -->
<div v-for="item in list" :key="item.id3">
<MyButton :foo="item">One</MyButton>
</div>
<!-- Case4 -- The key is not attached to _Fragment. -->
<template v-for="item in list" :key="item.id4">
<div>
<MyButton :foo="item">One</MyButton>
</div>
</template>
</div> |
I think that should be considered as a bug. |
<template v-for="item in list" :key="item.id1">
<MyButton :foo="item">One</MyButton>
<MyButton :foo="item">Two</MyButton>
</template> is compiled as the equivalent of <template v-for="item in list">
<Fragment :key="item.id1">
<MyButton :foo="item">One</MyButton>
<MyButton :foo="item">Two</MyButton>
</Fragment>
</template> Thus the need to change the ESLint rule. For a consistent template syntax, we should treat <template v-for="item in list" :key="item.id2">
<MyButton :foo="item">One</MyButton>
</template> as the equivalent of <template v-for="item in list">
<MyButton :foo="item" :key="item.id2">One</MyButton>
</template> , too. But that has not been implemented, so the results in the template explorer didn't meet expectations. |
It's not clear to me yet, so please let me know. |
Yes, it's Vue 3's bug. |
I get it! Thank you! |
Version
3.0.0-rc.5
Reproduction link
https://jsfiddle.net/Nate_L/0y3kbmaf/13/
Steps to reproduce
What is expected?
The code works without any warning in the console
What is actually happening?
The code works, but a warning is logged in the console (
[Vue warn]: Template compilation error: v-if branches must use compiler generated keys.
)Changing Vue to version vue@3.0.0-rc.4 fixes the bug.
Note that the warning happens no matter what is put inside the
v-if
, I tried with other reactive properties or to set it to true.I have other
v-if
inside my codebase which are not inside a<template>
withv-for
, and all of them works without issuing a warning.The text was updated successfully, but these errors were encountered: