【发布时间】:2020-10-06 06:37:15
【问题描述】:
我将 Nuxt.js 与 Vue.js 一起使用。我添加了这样的动态组件。
// pages/index.vue
<template>
<div class="filter-container">
<component v-for="item in buttons" :is="item" :key="item.id" :buttons="buttons" />
<span class="add-search" @click="add">ADD</span>
</div>
</template>
<script>
const dynamicComponentFilter = () => import("@/components/ProductFilter");
export default {
data() {
return {
buttons: []
}
},
methods: {
add() {
this.buttons.push(dynamicComponentFilter);
}
}
}
</script>
在组件中
// components/productFilter.vue
<template>
..........
<span class="del" @click="delQuerysToStore(item)">del</span>
..
..
..
</template>
<script>
export default {
props: ["buttons", "item"],
methods: {
delQuerysToStore(item) {
this.buttons.splice(this.buttons.indexOf(item), 1);
}
}
}
</script>
像这样。 当我单击添加按钮时,每次单击都会出现组件。 当我单击 del 按钮时,然后删除最后一个组件... 我想删除与我单击的按钮相对应的组件。
为什么 indexOf 不起作用?我看了console.log(this.buttons.indexOf(item));,它确实有效!当我单击第二个按钮时,出现索引 1。但是为什么在组件上不起作用?
【问题讨论】:
标签: javascript vue.js nuxt.js