:key on v-for 重新渲染列表中的一行 ||复合值作为 :key 而不是 index
:Key on v-for to rerender a row in the list || composite values as :key rather than index
最近,我遇到了一个问题,要解决这个问题,我需要在 v-for
中为 :key
提供唯一值。但问题是我没有唯一值。将索引指定为 :key
并不能解决我的问题。我的 v-for
数组是一个对象列表,其中一个对象是
{sourceValue:"1",targetValue:"2",assigned:true}
我的唯一值将是 sourceValue 和 targetValue 的组合...那么我应该如何在 v-for
上定义 :key
? :key="cfg.sourceValue+cfg.targetValue"
可以吗?我如何验证 DOM 上的键值?
v-for="(cfg, idCfg) in $store.state.configurations" :config="cfg" :key="idCfg"
用 +
连接两个值对我来说真的很管用。
<config-record v-for="(cfg, idCfg) in $store.state.configurations" :config="cfg" :key="cfg.sourceValue+cfg.targetValue"></config-record>
我已经在 Vue Dev 工具中进行了验证,发现 :key 值实际上是唯一的,并且是 sourceVal 和 targetVal 的串联值。
$store.state.configurations = [
{sourceValue:"TestDivya",targetValue:"3",assigned:true},{sourceValue:"TestDivya1",targetValue:"test",assigned:true},{sourceValue:"DEBUG_LOGGING",targetValue:"ENABLED",assigned:true}
]
最近,我遇到了一个问题,要解决这个问题,我需要在 v-for
中为 :key
提供唯一值。但问题是我没有唯一值。将索引指定为 :key
并不能解决我的问题。我的 v-for
数组是一个对象列表,其中一个对象是
{sourceValue:"1",targetValue:"2",assigned:true}
我的唯一值将是 sourceValue 和 targetValue 的组合...那么我应该如何在 v-for
上定义 :key
? :key="cfg.sourceValue+cfg.targetValue"
可以吗?我如何验证 DOM 上的键值?
v-for="(cfg, idCfg) in $store.state.configurations" :config="cfg" :key="idCfg"
用 +
连接两个值对我来说真的很管用。
<config-record v-for="(cfg, idCfg) in $store.state.configurations" :config="cfg" :key="cfg.sourceValue+cfg.targetValue"></config-record>
我已经在 Vue Dev 工具中进行了验证,发现 :key 值实际上是唯一的,并且是 sourceVal 和 targetVal 的串联值。
$store.state.configurations = [ {sourceValue:"TestDivya",targetValue:"3",assigned:true},{sourceValue:"TestDivya1",targetValue:"test",assigned:true},{sourceValue:"DEBUG_LOGGING",targetValue:"ENABLED",assigned:true} ]