javascript - Vue : delete an item from array when it is passed as a prop to a component?

标签 javascript vuejs2 vue-component

我对 Vue.js 非常陌生。

我首先搜索了如何执行此操作(从数组中删除元素,仅给出数组中的元素),但我发现的只是对 delete 方法或调用 的引用>slice 在列表中...这当然是相关的,但没有回答我的问题。

我在这里做了一个 fiddle :

https://jsfiddle.net/SumNeuron/d2a9j2mh/

理想情况下,我想单击删除来删除选项卡和选项卡 Pane 。

有没有办法在不将列表作为 Prop 传递的情况下执行此操作?

最佳答案

这是您的期望吗:https://jsfiddle.net/ittus/Lost5djd/

单击删除按钮时发出事件

  methods: {
    remove: function() {
      console.log(this, this.tab)
      this.$emit('remove', this.index)
    }
  }

并在父组件上监听

  methods: {
    onRemove(index) {
      this.groups =  [
       ...this.groups.slice(0, index),
       ...this.groups.slice(index + 1)
     ]
    }
  }

如果每个选项卡都有一个id,解决方案将会更加优雅。

关于javascript - Vue : delete an item from array when it is passed as a prop to a component?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50525376/

相关文章:

javascript - 将 VueJs 模态组件添加到 Laravel 模板

javascript - VueJS 获取数据作为对象

vue-component - 如何在 Vue 中公开包装的 <input>?

javascript - 随机化数据功能(最佳实践)

javascript - 通过mongoose访问数组元素

javascript - 使用 angular-ui-bootstrap 进行弹出

javascript - jQuery:类的 .nextUntil()

javascript - jQuery 计算给出 NaN

javascript - vue.js 获取 App.vue 中的路由名称

javascript - VueJs - 调用传递给动态创建的 DOM 元素的方法