如何获得删除按钮以删除一行数据?

时间:2020-06-20 20:34:36

标签: vue.js vuejs2

我做错了什么? 我正在尝试使用删除按钮删除一个人的数据。

<li v-for="(person,index) in persons">
  <span v-for="value in person"> {{ value }} </span>
  
</li>
 <button @click.native="deleteQuote">
  Delete
  </button>
  data: {
    ingredients: ['meat', 'fruit', 'salad'],
    persons: [
    {name: 'Max', age: 27, color: 'red'},
    {name: 'Mit', age: 47, color: 'red'},
    {name: 'Foo', age: 37, color: 'red'},
    ]
  },
  methods: {
    deleteQuote: function(person) {
        this.persons.splice(index, 1);
    }
  }
})

1 个答案:

答案 0 :(得分:0)

将按钮元素放在native标记内,并删除index修饰符,并将deleteQuote作为参数传递给<li v-for="(person,index) in persons"> <span v-for="value in person"> {{ value }} </span> <button @click="deleteQuote(index)"> Delete </button> </li> 方法:

index

该方法应以 methods: { deleteQuote: function(index) { this.persons.splice(index, 1); } } 作为参数来定义:

{{1}}