bounty还有6天到期。回答此问题可获得+50声望奖励。parsecer希望引起更多关注这个问题。
代码fiddlie:https://codesandbox.io/s/thirsty-austin-ty6f7i?file=/src/App.vue
我有一个简单的页面,允许用户更改3输入,并添加3图像:
App.vue:
<template>
<div>
<div v-for="(line, i) in this.lines" class="line-wrapper" v-bind:key="i">
<AddImageComponent
class="add-image"
matchType="image.*"
selectButtonText="SELECT OR DROP AN IMAGE"
/>
<input type="text" v-model="this.lines[i].text" />
<div class="delete-button" v-on:click.stop="this.lines.splice(i, 1)">
Delete
</div>
</div>
</div>
</template>
<script>
import AddImageComponent from "./AddImageComponent.vue";
export default {
name: "App",
components: {
AddImageComponent: AddImageComponent,
},
data() {
return {
lines: [{ text: "A" }, { text: "B" }, { text: "C" }],
};
},
};
</script>
<style>
#app {
background-color: darkcyan;
height: 100vh;
}
.add-image {
background-color: lime !important;
height: 50%;
}
.line-wrapper {
background-color: purple;
border: 2px solid black;
margin-top: 2em;
}
.delete-button {
background-color: red;
padding: 5px;
max-width: 50%;
border-radius: 10px;
border: 2px solid red;
}
.delete-button:hover {
cursor: pointer;
background-color: transparent;
border: 2px solid red;
color: white;
}
input {
margin: 10px;
background-color: yellow;
padding: 5px;
border-radius: 10px;
border: none;
}
</style>
我看到的第一页是这样的:
当我添加图像时:
当我点击第一个(最上面的)Delete
红色按钮时:
因此,当剩余元素的文本是B
和C
(文本A
被删除)时,图片是鸟和蛇,而不是蛇和狗(最后一个AddImageComponent
被删除,而不是第一个)。
我不确定解决这个问题的最佳实践方法是什么。
2条答案
按热度按时间6mzjoqzu1#
假设
key
不是line
的数组索引。为每个line
成员添加一个唯一的id
成员,并将其用作key
属性:活生生的例子
46scxncf2#
正如我在评论中提到的,使用key不是问题,而是如何使用它。将key绑定到迭代的index值并不是一件理想的事情。在模板中使用splice和this关键字在这种情况下也不理想。
注意你的脚本部分。lines是一个对象数组,text是唯一的属性。理想情况下,应该有一个唯一的属性来绑定key,例如id。所以我会提出两个解决方案
请注意,我删除了模板中的this关键字,并将key绑定到行中每一项的text属性。还要注意我在脚本中提供的onDelete方法。
现在,此解决方案假设您在行中的唯一属性为text。另一种方法是使用id作为唯一属性,并使用它代替text。总之,这里的解决方案还注意到我起飞的v型在使用中。它实际上会给你带来麻烦。在循环中使用v-model有几种方法,但不是你处理它的方式。我建议你在这里做一些阅读https://vuejs.org/guide/essentials/event-handling.html
除了这次之外,其他解决方案都是相同的。我正在修改lines,使其具有唯一属性,例如id。我个人建议你遵循这种方法。
对于文档和参考资料,您可以查看vue文档。https://vuejs.org/guide/introduction.html