How to remove an item from an array in Vue.js

前端 未结 8 1123
小蘑菇
小蘑菇 2020-11-29 02:24

I am new to vue.js (2) and I am currently working on a simple event app. I\'ve managed to add events but now I would like to delete events based on clicking on a button.

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 02:38

    It is even funnier when you are doing it with inputs, because they should be bound. If you are interested in how to do it in Vue2 with options to insert and delete, please see an example:

    please have a look an js fiddle

    new Vue({
      el: '#app',
      data: {
        finds: [] 
      },
      methods: {
        addFind: function () {
          this.finds.push({ value: 'def' });
        },
        deleteFind: function (index) {
          console.log(index);
          console.log(this.finds);
          this.finds.splice(index, 1);
        }
      }
    });
    
    

    Finds

    {{ $data }}

提交回复
热议问题