Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

javascript - I'm having trouble creating a ToDo-list using Vue.js

I want to delete an item when clicking a button, but it just doesn't delete the item.

Here's my code:

new Vue({
  el: "#app",
  data: {
    newItem: "Hello Vue.js!",
    items: []
  },
  methods: {
    addItem: function() {
      this.items.push({
        id: this.items.length + 1,
        name: this.newItem,
        completed: false
      })
      this.newItem = '';
    },
    toggleCompleted: function(item) {
      item.completed = !item.completed;
    }
  },
  deleteItem: function(itemID) {
    this.items.splice(itemID, 1);
  }
})
body {
  background: whiteGhost;
  text-align: center;
}

h1 {
  color: #2c3e50;
  font-family: Avenir;
  -webkit-text-stroke-width: 1px;
  -webkit-text-stroke-color: #1b2d40;
}

h3 {
  color: #222;
  font-family: Avenir;
  font-size: 15px;
  margin-top: -15px;
}

input {
  border-radius: 2px;
  font-family: Avenir;
  text-align: center;
}

button {
  border: outset rgb(20, 134, 245) 2px;
  border-radius: 50px;
  background: dodgerBlue;
  color: #2c3e50;
  padding: 0 8px 0 8px;
}

#deleteBtn {
  border: solid rgb(168, 24, 24) 1px;
  border-radius: 50px;
  background: fireBrick;
  color: #2c3e50;
  font-family: Avenir;
  padding: 1px 5px 0 5px;
}

#app {
  width: 100%;
  height: 100%;
  background: linear-gradient( -45deg, darkViolet, darkViolet, darkViolet, violet, violet);
  border-radius: 5px;
  background-size: 300% 200%;
  box-shadow: 5px 10px #ccc;
  animation: gradient 14s infinite linear;
  padding-bottom: 5px;
}

@keyframes gradient {
  0% {
    background-position: 0 0;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 0;
  }
}

p {
  font-family: Avenir;
}

button:disabled {
  opacity: 0.5;
}

.completed {
  text-decoration: line-through;
  opacity: 0.5;
}

#deleteBtn,
p {
  display: inline;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<div id="app">
  <h1>ToDo-list</h1>
  <h3>Organize your tasks!</h3>
  <input v-model="newItem" placeholder="I have to..."> <button @click="addItem" :disabled="newItem.length === 0">Add</button><br>
  <div v-for="item in items">
    <p :class="{completed : item.completed}" @click="toggleCompleted(item)">{{ item.name }}</p> <button id="deleteBtn" @click="deleteItem(item.id)"> X</button>
  </div>
</div>
question from:https://stackoverflow.com/questions/65877137/im-having-trouble-creating-a-todo-list-using-vue-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have an extra closing curly brace after the toggleCompleted method, so deleteItem isn't declared in the methods block.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...