Transition-group children must be keyed...but they are keyed(过渡组的孩子必须有钥匙……但他们有钥匙)
问题描述
尝试在我的组件模板中使用 <animation-group>
,但出现错误:
Trying to use <animation-group>
in my component template, but getting error:
[Vue 警告]:<transition-group>子项必须键入:<div>
但我很确定它们是有键的.
But I'm pretty sure that they are keyed.
//js
Vue.component('instruments', {
template: `
<transition-group name="fade">
<div class="instruments">
<div class="instrument" v-for="(instrument, index) in filteredInstruments" v-bind:key="index">
<img v-bind:src="instrument.photo">
<span class="name">{{ instrument.name }}</span>
<span class="construction">{{ instrument.top }} / {{ instrument.backAndSides }}</span>
<span class="price">$ {{ instrument.price }}</span>
</div>
</div>
</transition-group>
`
}
我认为设置 v-bind:key="index"
可以满足这一点,但我得到了上面粘贴的错误.
I think that setting v-bind:key="index"
would take satisfy this, but I get the error pasted above.
推荐答案
您必须为您的
<div class="instruments">
提供唯一的密钥元素,因为<transition-group>
中的 元素,特别是直接子元素,总是需要具有唯一键属性.
You have to give a unique key to your
<div class="instruments">
element since elements inside a<transition-group>
, specifically the immediate children, are always required to have a unique key attribute.
如果您不想为 .instruments
提供密钥,则可以删除该元素并分配 tag
和 class
属性改为 <transition-group>
因为它呈现一个实际元素,默认情况下是 <span>
.
If you don't want to give a key to .instruments
, you can remove that element and assign a tag
and class
attributes to <transition-group>
instead since it renders an actual element which by default is a <span>
.
<transition-group name="fade" tag="div" class="instruments">
通过这种方式,警告将不再出现,因为直接子级 (.instrument
) 已为其分配了唯一键.
In this way, the warning would not appear anymore since the immediate children (.instrument
) have their unique keys assigned to them.
这篇关于过渡组的孩子必须有钥匙……但他们有钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:过渡组的孩子必须有钥匙……但他们有钥匙
基础教程推荐
- 在for循环中使用setTimeout 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01