Advertisement
supermaca

Untitled

Mar 24th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <script>
  2. // give each todo a unique id
  3. let id = 0
  4.  
  5. export default {
  6. data() {
  7. return {
  8. newTodo: '',
  9. todos: [
  10. { id: id++, text: 'Esta es una tarea' },
  11. { id: id++, text: 'Tarea 2' },
  12. { id: id++, text: 'Tarea 3' }
  13. ]
  14. }
  15. },
  16. methods: {
  17. addTodo() { //definir addTodo method
  18. if (this.newTodo.trim()) { //after trimming ver si no es un empty string
  19. this.todos.push({ //agregar a array de todos
  20. text: this.newTodo
  21. });
  22. }
  23. this.newTodo = '';
  24. },
  25. removeTodo(todo) {
  26. let index = this.todos.indexOf(todo);
  27.  
  28. // console.log(index);
  29. this.todos.splice(index, 1);
  30. }
  31. }
  32. }
  33.  
  34. </script>
  35.  
  36. <template>
  37. <form @submit.prevent="addTodo">
  38. <input v-model="newTodo">
  39. <button>Add Todo</button>
  40. </form>
  41. <ul>
  42. <li v-for="todo in todos" :key="todo.id">
  43. {{ todo.text }}
  44. <button @click="removeTodo(todo)">X</button>
  45. </li>
  46. </ul>
  47. </template>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement