C
C
CoyoteSS2019-06-11 06:59:46
Vue.js
CoyoteSS, 2019-06-11 06:59:46

Where is the error in ToDo List on Vue?

There is an HTML index file with Vue connected via CDN.
New tdlist tasks are added with a bang, but the problem with deleting the last task, it simply does not delete.
I can't figure it out, please help.

<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="root">
  <ul>
    <li v-for="on in list" v-bind:key="on.key">{{ on.title }}<button v-on:click="deleteList(on.key)">Delete</button></li>
  </ul>
  <input v-model="gulp">
  <br>
  <button v-on:click="addList">Add</button>
</div>
<script>
  const vm = new Vue({
    el: '#root',
    data: {
      list: [],
      keys: 0,
      gulp: '',
    },
    methods: {
      addList: function() {
        this.list.push({ title: this.gulp, key: this.keys++ });
      },
      deleteList: function(num) {
        this.list.splice(num, 1);
      },
    },
  });
</script>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gribanov, 2019-06-11
@CoyoteSS

This is
turn into it

<li v-for="(on, index) in list" v-bind:key="on.key">{{ on.title }}<button v-on:click="deleteList(index)">Delete</button></li>

C
coderisimo, 2019-06-11
@coderisimo

1) everything worked and so
2) you do not need to generate the key manually

key: this.keys++
you can use the ready-made key index: 3) when adding a new element, it is worth clearing the input field, otherwise you have to clear the field manually all the time

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question