vuejs返回数据组件如何拼接子数组?

how to splice a subarray from returned data component vuejs?

我有一个对象,它是一个数组列表...

..这是在 vue js 中声明的数据,来自 prop:

    data: function(){
    return{
        listaSelezionati:this.selezionati
    }
}

..wich 在 v-for

上呈现
<div class="row" style="margin-bottom:20px;" v-for="(selezionato,index) in listaSelezionati">

..在这个 for 循环中有一个调用函数的按钮

<div class="remove pull-right" v-on:click="rimuovi(index)"></div>

..用这个函数我想用"index"拼接对象"listaSelezionati"的子数组,但我不知道该怎么做..

这是我尝试过的:

    methods:{
    rimuovi : function(index){
        alert(index);
        return{
            this.listaSelezionati[index][0].splice(index,1)
        }
    }
}

但它什么也没做,有人有什么建议吗?

edit1 我想知道 return{this.listaSelezionati 是否应该是编辑组件数据的正确方法

看来您对数组的了解太深了。

this.listaSelezionati[index]

是一个数组但是

this.listaSelezionati[index][0]

看起来像数组的第一个元素,它是一个对象。所以现在看起来你在一个位置索引处拼接一个对象。这应该会产生一个错误。 我想你需要:

rimuovi : function(outterIndex, innerIndex){
    alert(outterIndex, innerIndex);
    return{
        this.listaSelezionati[outterIndex].splice(innerIndex,1)
    }
}

<div class="remove pull-right" v-on:click="rimuovi(index, innerIndex)"></div>
// or
<div class="remove pull-right" v-on:click="rimuovi(outterIndex, innerIndex)"></div>

我还没有测试过,但至少它应该让你更接近

如果你想通过索引删除整个子数组,你需要做的就是:

methods:{
  rimuovi : function(index){
    this.listaSelezionati.splice(index,1)
  }
}

不需要return一个值; splice 更新调用它的数组。