vue.js - 删除索引 0 处的组件删除所有项目
问题描述
我在循环中有一个组件,每个组件都有一个删除按钮。
如果我删除索引 1 及以上的组件,它会很好地删除,但是当我删除索引 0(第一个)处的项目时,所有组件都会消失,但数据在 Vue 控制台中仍然可用。
//form.vue
<template>
<div>
<div
v-for="(item, index) in items"
:key="index"
>
<viewer
:item="item"
:index="index"
@removed="remove"
></viewer>
</div>
</div>
</template>
<script>
import default {
//truncated
data() {
items: ['sample 1', 'sample 2', 'sample 3']
},
methods: {
remove(index) {
this.items.splice(index,1)
}
}
}
</script>
//viewer.vue
<template>
<div>
<div>{{ item }}</div>
<div @click="remove()">Remove</div>
</div>
</template>
<script>
import default {
//tuncated
props: {
item,
index
},
methods: {
remove() {
this.$emit("removed", this.index)
}
}
}
</script>
解决方案
你有几个错误:
v-for
应该这样写:
<div
v-for="(item, index) in items"
:key="index"
>
props
应该这样定义:
props: ["item", "index"],
data()
应该这样写:
data() {
return {
items: ["sample 1", "sample 2", "sample 3"]
};
},
推荐阅读
- asp.net-mvc - 从池中获取流
- regex - 需要帮助从列表中提取数据模式,如下所示
- python - 如何过滤python中文本文件第一行中的字段名
- ajax - 在两个面板组上使用 p:ajax 时,Omnifaces validateOrder BigDecimal 无法转换为字符串
- android - 如何在 Appium 中单击特定的 WebElement
- php - 无法让 sweetalert2 ajax 工作
- java - Android工作室设计模式
- php - PHP单元中方法的模拟部分
- powershell - 将递归找到的文件传递到 cmdlet
- android - 带有数据库和网络请求的分页库