vuejs2 - 使用单击事件时,如何以编程方式从 DOM 中删除呈现的 v-for 元素?
问题描述
我正在尝试使用V-Animate-CSS在按下删除按钮时显示“删除”动画。我正在努力尝试指定要在 v-for 循环中以编程方式删除的确切元素。
让我解释:
我有以下vue <template>
:
<div
v-for="x in divisionLangs"
:key="x.P_uID"
>
<button
type="button"
@click.prevent="deleteCard(x.P_uID)"
>
</button>
<transition name="bounce"
<div v-if="show" class="card-body">
<!-- card content is here -->
</div>
</transition>
我的<script>
部分如下所示:
data() {
return {
show: true,
divisionLangs: []
}
}
deleteCard(id) {
this.show = !this.show
this.divisionLangs = this.divisionLangs.filter(x => x.P_uID !== id)
},
divisionLangs
数组的数据如下所示:
[
{
P_uID: 789,
..blah...
},
{
P_uID: 889,
...blah...
}
]
如何构造此代码,以便仅从呈现列表中删除匹配的卡片项目,而不是所有卡片项目?现在发生的事情是该deleteCard
方法上的所有卡片都被删除了。
解决方案
您应该为每个项目定义一个子组件,如下所示:
父组件:
<tmplate>
<child-component v-for="x in divisionLangs" :key="x.P_uID" />
</template>
在子组件中:
<button
type="button"
@click.prevent="deleteCard(x.P_uID)"
>
</button>
<transition name="bounce"
<div v-if="show" class="card-body">
<!-- card content is here -->
</div>
</transition>
子组件的脚本部分:
data() {
return {
show: true,
divisionLangs: []
}
}
deleteCard(id) {
this.show = !this.show
this.divisionLangs = this.divisionLangs.filter(x => x.P_uID !== id)
},
推荐阅读
- c# - 如何使用 HttpClient 配置 json 反序列化?
- php - 为什么 WordPress 以不同于常规字符串的方式转换我的帖子标题?
- regex - 如何用python获取字符串中每个字符的位置?
- c# - 如何从 Outlook 中的内联回复或新回复窗口获取原始电子邮件?C#
- ios - 如何从 firebase 数据库中检索特定值并将其分配给 Swift 4 中的特定变量?
- c# - 插入实体框架子项的顺序
- python - 使用 Django 从大型数据库中更快地检索数据
- sparql - 在 wikidata 中按类型过滤属性值
- c++ - 为什么共享内存(在 ipc 中)不需要上下文切换?它是来自内核空间的内存映射到用户空间吗?
- php - Firebase (FCM) + Laravel | 推送通知错误 InvalidApnsCredential