【发布时间】:2021-12-08 23:00:50
【问题描述】:
我正在使用 BootstrapVue。
单击parent.vue 中的按钮后,我想splice 我在child.vue 中的输入。
但每次我删除 parent.vue 中的某些内容时——我可以保证它正在工作——只有 child.vue 中的最后一个 Input 元素会被删除。
如果你想尝试一下(你可以复制粘贴代码并运行它):
- 添加 3 个输入
- 打开每个 Input 并在其中写入一个数字,例如
Input 1 -> 1111、Input 2 -> 2222和Input 3 -> 3333(您也可以添加更多 Input Informations,但在这里并不真正相关) - 例如删除第二个输入,通常它现在应该像
Input 1 -> 1111、Input 2 -> 3333但它总是Input 1 -> 1111和Input 2 is still 2222,因为它总是删除最后一个输入信息..
我该如何解决这个问题,正确的输入信息也会被删除?
非常感谢!
更新:
错误是我的parent.vue 中的index 在删除某些内容后发生了变化,但在我的child.vue 中它没有删除正确的index,也没有重新创建所有其他indexes
我的 parent.vue 中的代码:
<template>
<div>
<div class="inputArea mt-2" v-for="(id, indexParent) in inputs" :key="indexParent">
<div class="col-md-12">
<b-button-group>
<b-button class="col-md-6" v-b-toggle="'newInput' + indexParent" variant="danger">Input</b-button>
<b-button class="col-md-6" @click="deleteThis(indexParent)" variant="danger">Delete</b-button>
</b-button-group>
</div>
<child :key="indexParent" :indexParent="indexParent" ref="ChildComponent" />
</div>
<div class="mt-3 mb-3 ml-3 mr-3">
<b-button @click="addThis()" variant="success">Add Input</b-button>
</div>
</div>
</template>
<script>
import child from './child.vue'
export default {
name: "parent",
components: {
child,
},
data() {
return {
inputs: [{}],
};
},
methods: {
deleteThis(indexParent) {
this.inputs.splice(indexParent, 1);
this.$refs.ChildComponent[indexParent].deleteThisFromParent(indexParent);
},
addThis() {
this.inputs.push({});
},
},
};
</script>
我的孩子.vue:
<template>
<b-collapse visible :id="'newInput' + indexParent" class="mt-2">
<div v-for="(id, indexChild) in inputs" :key="indexChild">
<table class="table table-striped mt-2">
<tbody>
<h5 class="ml-1">Input Informations</h5>
<tr>
<div class="mt-2">Input</div>
<b-form-input></b-form-input>
</tr>
</tbody>
</table>
</div>
<b-button @click="addInputInfo()">Add Input Informations</b-button>
</b-collapse>
</template>
<script>
export default {
name: "child",
methods: {
deleteThisFromParent(indexParent) {
console.log(this.inputs); //Here I get the correct input which I want to delete
console.log(indexParent); //correct index of parent.vue
this.inputs.splice(); //here it deletes the last Input everytime..
},
addInputInfo() {
this.inputs.push({});
},
},
props: ["indexParent"],
data() {
return {
inputs: [{}],
};
},
};
</script>
【问题讨论】:
标签: javascript vue.js vuejs2 components