简体   繁体   English

如何删除 Nuxt.js 中的动态组件

[英]How to delete dynamic component in Nuxt.js

I used Nuxt.js with Vue.js.我使用 Nuxt.js 和 Vue.js。 I added dynamic component like this.我添加了这样的动态组件。

// 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>

and in the component并在组件中

// 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>

like this.像这样。 when I click add button, then appear component per click.当我单击添加按钮时,每次单击都会出现组件。 and when I click del button, then delete last component... I want to delete the component that corresponds to the button I clicked.当我单击 del 按钮时,然后删除最后一个组件...我想删除与我单击的按钮相对应的组件。

Why didn't work indexOf?为什么 indexOf 不起作用? I watch console.log(this.buttons.indexOf(item));我看console.log(this.buttons.indexOf(item)); , it did work, When I click second button. ,它确实有效,当我单击第二个按钮时。 then appear index 1?然后出现索引1? But Why didn't work at component?但是为什么不在组件上工作?

I think you do not have access to this.buttons in components/productFilter.vue because this.buttons is scoped to its component instance and it's not a good practice to provide it in props and to expect reactivity in its parent.我认为您无权访问components/productFilter.vue this.buttons的 this.buttons ,因为this.buttons的范围仅限于其组件实例,并且在 props 中提供它并期望其父级中的反应性并不是一个好习惯。 I would suggest you to pass the index of this dynamic component in it via props item-id and then to emit an event in delQuerysToStore() and after that to remove the component in the parent pages/index.vue .我建议您通过 props item-id在其中传递此动态组件的索引,然后在delQuerysToStore()中发出一个事件,然后删除父pages/index.vue中的组件。 Example:例子:

// components/productFilter.vue

 props: {
   itemId: {
     type: Number,
     default: 0
   }
},
 methods: {
  delQuerysToStore(item) {
      this.$emit('delete', this.itemId)
    }
  }
}
// pages/index.vue

<template>
  <div class="filter-container">
    <component v-for="(item, index) in buttons" :is="item" :key="item.id" :buttons="buttons" @delete="deleteComponent" :item-id="index"/>
    <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);
    },
    deleteComponent(itemId) {
     this.buttons.splice(itemId, 1)
    }
  }
}
</script>

In general this is the standard and good practice: Data down -> actions up.一般来说,这是标准和良好的做法:数据向下 -> 动作向上。 You provide data down to lower level components, but the lower level components emit events to their parent and thus the communication work between components.您将数据提供给较低级别的组件,但较低级别的组件会向其父级发送事件,因此组件之间的通信工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM