Removing items in vue.js

I'm currently in the process of learning Vue.js. This is my first attempt at creating a small to-do application, and I am encountering issues with deleting each individual task upon clicking. Despite watching multiple YouTube tutorials, I have not been able to resolve the problem. Any assistance would be greatly appreciated! I believe the issue lies in properly passing through the $emit and the id.

If you know of any helpful tutorials, please feel free to share them!

App.vue

<template>
  <Tasks :tasks="tasks" @delete-task="deleteTask" />
</template>

<script>
import Tasks from "./components/Tasks.vue";

export default {
  name: "App",
  components: {
    Tasks,
  },
  data() {
    return {
      tasks: [],
    };
  },
  methods: {
    deleteTask(id) {
      this.tasks = this.tasks.filter((task) => task.id !== id);
    },
    addTask(newTask) {
      this.tasks = [...this.tasks, newTask];
    },
  },
  created() {
    this.tasks = [
      {
        id: 0,
        text: "Grocery shopping",
        date: "March 25, 2022",
        reminder: true,
      },
      {
        id: 1,
        text: "Doctors Appointment",
        date: "March 30, 2022",
        reminder: true,
      },
      {
        id: 2,
        text: "Pay Bills",
        date: "April 1, 2022",
        reminder: false,
      },
    ];
  },
};
</script>

Tasks.vue

<template>
  <div>
    <Task :tasks="tasks" @delete-task="$emit('delete-task')" />
  </div>
</template>

<script>
import Task from "./Task.vue";

export default {
  name: "Tasks",
  emits: ["delete-task"],
  components: {
    Task,
  },
  props: {
    tasks: Array,
  },
};
</script>

Task.vue

<template>
  <div v-for="task in tasks" :key="task.id" @click="onDelete(task.id)">
    <p>{{ task.text }}</p>
    <p>{{ task.date }}</p>
  </div>
</template>

<script>
export default {
  name: "Task",
  emits: ["delete-task"],
  props: {
    tasks: Object,
  },
  methods: {
    onDelete(id) {
      this.$emit("delete-task", id);
    },
  },
};
</script>

Answer №1

Error in Tsaks.vue: No ID provided to App.vue component

<template>
 <div>
<Task :tasks="tasks" @delete-task="$emit('delete-task')" />
</div>
</template>
===>
<template>
 <div>
 <Task :tasks="tasks" @delete-task="(id)=>$emit('delete-task',id)" />
 </div>
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is Vue Router Navigation Guard throwing an error about exceeding the maximum call stack size?

Encountering a recurring issue of maximum stack size exceeded while implementing the following code for vue router navigation guards per-route: import state from "../vuex-store/state.js"; import Editor from "../views/Editor"; const routes = [ { ...

Step-by-step guide on creating a monochrome version of an image using automation

In WordPress, I have a logo parade where all logos are in color RGB. I really like the effect of having them appear as black and white initially and then transitioning to color when hovered over. I am familiar with using sprites for this effect, but it wo ...

AJAX: Building a Robust Single Page Application with Enhanced Security

Currently, I am developing a web/mobile application using AJAX. This app consists of 4 pages: the login page and three protected pages that are only accessible to logged-in users. My plan is to implement the Single Page Application pattern, where all 4 pa ...

Endless Scroll Feature in Javascript

A brief tale: I am in search of a way to extract data from a website that features infinite scroll without actually implementing the infinite scroll feature myself. My plan is to create a script that will auto-scroll the page from the browser console, wai ...

Incorporating External HTML Content Using JQuery Function

Looking for assistance with a JQuery function: function addSomeHTML() { $("#mysection").html("<div id='myid'>some content here</div>"); } I am trying to have this part: <div id='myid ...

Which specific technological platform or framework would be most suitable for constructing a similar project?

https://i.stack.imgur.com/LL1g9.png Looking at the image provided, my goal is to allow users to navigate between pages on the Home page without having to refresh the entire browser window. I believe this can be achieved using Ajax technology, am I correct ...

Having difficulty importing app.js into other modules within ExpressJS

Having trouble importing app.js into other modules in ExpressJs. It imports successfully, but I can't use the functions defined in the app.js file. The code snippet I am working with is as follows: I have this app.js var app = express(); var express ...

Javascript favorite star toggling

An excellent illustration is the star icon located on the left side of this post. You have the ability to click on it to save this message as a favorite and click again to remove the flag. I have already set up a page /favorites/add/{post_id}/, but I am ...

Shifting the entire page from left to right and back again

I am in search for a template that moves the page from left to right. If anyone can guide me on how to create this effect or provide a JavaScript example, I would greatly appreciate it. ...

Is there a way to reset back to the default CSS styles?

I have a div container with a nested span element (simplified). <div class="divDash"> <span>XX</span> </div> The CSS styling for the div makes the span initially hidden and only visible when hovering over the parent div. .div ...

Issues with Node.js and AJAX

I am having trouble with my ajax request to retrieve data from node.js. The interaction between the two seems off, but I can't pinpoint where the issue lies. Below is the snippet of my ajax get request: $.get('/notificationsNumber', ...

Retrieve pairs of items from a given variable

Containing values in my 'getDuplicates' variable look like this: getDuplicates = 100,120,450,490,600,650, ... These represent pairs and ranges: Abegin,Aend,Bbegin,Bend My task is to loop through them in order to apply these ranges. var ge ...

Sending a multitude of variables using strings, evaluating them through various functions, and incorporating a variety of methods

To put it simply, my goal is to utilize an object literal that allows me to pass an unknown quantity of variables in any order to a function. While this may seem straightforward in principle, within my code, this object literal is passed to a second functi ...

Unable to generate new entries with HTML Form

I've been working on creating a simple form with the ability to add new seasons or entries that will be posted to a database, but I've hit a roadblock. Whenever I try to run it, the "Add more Episodes" buttons for new seasons don't seem to w ...

Passing a node.js variable to an ejs template through a post request

In my nodejs application, I utilize Express JS to handle post requests. Within the INDEX.JS FILE, the following code snippet is present: this.app.post('/profile', (req, res, next) => { let password = req.bo ...

Access information from an array in Angularjs and transfer it to an identifier

I am facing an issue with passing values from the view to the controller and storing them in an array. My goal is to then retrieve a value from the array and pass it as the id value in the update method. Here is my current setup: HTML <label class="c ...

Creating a Countdown in Javascript Using a Variable

I want the date to change from the first date to the second date. At the start, it should display 'Starts:' in bold followed by the remaining time. Once it switches to the second date, it should show 'Ends:' in bold and then the remaini ...

Utilizing Vue JS methods across different blade views

Utilizing Vue in conjunction with Laravel, I have adopted a structure similar to this: Currently, each blade view has its own dedicated JavaScript file. However, I now face the challenge of efficiently reusing these JavaScript files across multiple blade ...

When working with an Angular application, IE9 may display the error message: "An internal error occurred in the Microsoft Internet extensions"

My application is encountering an issue in IE9: Error: A Microsoft Internet Extensions internal error has occurred. Error: Access is denied. When using IE's dev tools for debugging, the issue seems to be related to localStorage. if (localStorage) ...

The getHours function seems to be malfunctioning when calculating the difference between dates

[code][1] Hello, I am currently working on calculating the difference between two dates and I want the result to be displayed in the format 00:00:00. Currently, my "current" time is set as 00:00:00 but I need it to dynamically update based on the hours, m ...