Я прочитал документацию для рендеринга пользовательских компонентов в списке, используя v-for здесь .
Но по какой-то причине я не могу заставить это работать. Он всегда удаляет последний компонент вместо того, который я отправляю в индексе. Есть идеи, почему он не работает?
Моя версия VUE JS: 2.5.16.
Использование PHPStorm IDE и запуск в Docker (контейнер Linux)
И Laravel mix (у меня есть "laravel-mix": "0. *" запись в package.json) для использования webpack и компиляции модулей JS.
Вот часть моего кода
// Parent Component JS
<template>
<ul>
<li
is="child-component"
v-for="(child, index) in componentList"
:key="index"
:myVal="Something...."
@remove="dropField(index)"
@add-custom-field="addField"
></li>
</ul>
</template>
<script>
import childComponent from './ChildComponent';
export default {
name: 'CustomList',
components: {'child-component' :childComponent},
data() {
return {
componentList: []
}
},
methods: {
addField() {
console.log('Handling add-custom-field field...');
this.componentList.push(childComponent);
},
dropField(index) {
console.log(`I am deleting the component with index = ${index} from listview in parent...`);
this.componentList.splice(index, 1);
}
}
}
// Child Component JS
<template>
<div>
<input type="text" v-model="currentValue" /><button @click.prevent="$emit('remove')" > Remove </button>
</div
</template>
<script>
export default {
props: { myVal : '' },
data() { return { currentValue: ''} },
created() {this.currentValue = this.myVal;}
}
</script>