Troubleshooting issue: Unable to remove objects from an array using Vue.js

Having trouble with a Vue.js project where I'm attempting to delete an object from the items array by clicking the remove button in the CheckList.vue file. However, I keep encountering this error message: Property or method "remove" is not defined on the instance but referenced during rendering. Could someone provide assistance?

Example.vue

 <template>
  <div>
    <input type="text" v-model="message" @keyup.enter="add">
    <button @click="add">Add Item</button>
    <CheckList :items="items" all="all" title="All Items"></CheckList>  
    <CheckList :items="doneItems" title="Done Items"></CheckList>
    <CheckList :items="notDoneItems" title="Not Done Items"></CheckList>
  
  </div>
</template>

<script>
import CheckList from "./CheckList";
export default {
  name: "Example",
  components: {CheckList},
  data() {
    return {
      message: '',
      items: [
        {name:'Apple', done: true, key: 0},
        {name:'Orange', done: false, key: 1},
        {name:'Grapes', done: true, key: 2},
      ],
      
    }
  },
  methods: {
    add(){
      if(this.message !== '') {
        this.items.push({
          name: this.message,
          done: false,
          key: this.items.length
        });
        this.message = '';
      }
    
    },
    remove(index){
        this.events.splice(index, 1);
    }
    
    
  },
  
  computed: {
    doneItems(){
      return this.items.filter(item => item.done);
    },
    notDoneItems(){
      return this.items.filter(item => !item.done);
    }
  }
}
</script>

<style scoped>

  
  
</style> 

CheckList.vue

<template>
  <div>
    <h3>{{ title }}</h3>

    <ul>
      <li v-for="(item,key) in items" :key="item.key">
        <input type="checkbox" v-model="item.done">
        {{item.name}}
        <button v-if="all" v-on:click="remove(key)" class="button1">remove</button>
      </li>
    </ul>
    
    
  </div>
</template>

<script>
export default {
  name: "CheckList",
  props: ['items', 'title', 'all']
}
</script>

<style scoped>
.button1{
    margin-left:10px;
  }
</style>

Answer №1

Check out this example:

Vue.component('check-list', {
  template: `
    <div>
      <h3>{{ title }}</h3>
      <ul>
        <li v-for="(item,key) in items" :key="item.key">
          <input type="checkbox" v-model="item.done">
          {{item.name}}
          <button v-if="all" v-on:click="$emit('remove',key)" class="button1">remove</button>
        </li>
      </ul>
    </div>
  `,
  props: ['items', 'title', 'all'],
  methods: {
    remove() {
      $emit('remove', 'key')
    }
  }
})

new Vue({
  el: '#demo',
  data: {
    message: '',
    items: [
      {name:'Apple', done: true, key: 0},
      {name:'Orange', done: false, key: 1},
      {name:'Grapes', done: true, key: 2},
    ],
  },
  computed: {
    doneItems(){
      return this.items.filter(item => item.done);
    },
    notDoneItems(){
      return this.items.filter(item => !item.done);
    }
  },
  methods: {
    add(){
      if(this.message !== '') {
        this.items.push({
          name: this.message,
          done: false,
          key: this.items.length + 1
        });
        this.message = '';
      }
    
    },
    remove(index){
        this.items.splice(index, 1);
    }
    
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div>
    <input type="text" v-model="message" @keyup.enter="add">
    <button @click="add">Add Item</button>
    <check-list :items="items" all="all" title="All Items" @remove="remove" />
  </div>
</div>

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

What are some effective methods for drawing attention to newly added table rows?

Whenever I input new data into my table, the data does not get highlighted as expected. However, the existing data in the table gets highlighted with no issues. Can you please help me troubleshoot why my code is not functioning correctly? Thank you. The f ...

Simple Steps to Convert an HTML String Array into Dynamic HTML with AngularJS Using ng-repeat

Below is an array consisting of HTML strings as values. How can I convert these strings into executable HTML code to display them? [ {html:'<button>name</button>'}, {html:'<table > <thead> <tr> <th>#</ ...

I am implementing a new method in the prototype string, but I am uncertain about its purpose

I am trying to wrap my head around the concept here. It seems like the phrase will pass a part of an array, in this case eve, to the phrase.palindrome method. This method will then process it. First, the var len takes the length of eve and subtracts 1 from ...

Utilizing Apollo and Vue.js to make GraphQL queries with multiple aliases

My Strapi backend is causing me some trouble as I attempt to fetch data from a single collection type into my Vue.js project using Apollo. Everything works smoothly with a single alias, but I'm facing difficulties when trying to work with multiple ali ...

Encountering a RangeError when transmitting OpenLayers3 event via AJAX

An error is appearing in the chrome console stating: RangeError: Maximum call stack size exceeded The code I am using is as follows: draw.on('drawend', function(evt) { var fe = evt.feature console.log(fe); ...

Determining the Existence of a Specific Bot within a Guild | Utilizing discord.js

Hey there! I've got a bot that needs to collaborate with two other bots. I'm looking for a way to check if one of the bots is already in the guild. If it's not, I want to prompt the user to invite it. Here's what my code currently look ...

Easily changing the state of a specific array item using React

I'm having trouble updating the state of a specific item in an array called reviews. Can someone guide me on how to achieve this? The following code snippet is not working as expected: this.setState({ reviews[2].current: true }); Below is the comp ...

Using JavaScript or another programming language to relocate files on a desktop

Is there a way to move pictures from the desktop to a folder called "pictures" using JavaScript? If not, perhaps through Ajax, PHP, or HTML? Is it possible to achieve this task by any means? Edit: I prefer not to do it on my server for web users. Is it f ...

Guide to showing MySQL data on an HTML page using NodeJS

Currently delving into Node JS, I am faced with the challenge of displaying fetched data from my MySQL Table in an HTML table format. Despite my efforts, I have yet to find a solution that works for me. Any help or guidance on how to resolve this issue wou ...

Tips for creating text that adjusts to the size of a div element

I'm currently working on developing my e-commerce website. Each product is showcased in its own separate div, but I've encountered a challenge. The issue arises when the product description exceeds the limits of the div, causing it to overflow ou ...

Struggling with incorporating elements into a dynamic CSS3 banner design?

Struggling to add up to 10 text items to the CSS3 animated banner. Having difficulty getting the items to display properly. I'm curious if there is a simpler way to achieve this with animations as shown in this example: http://jsfiddle.net/zp6B8/1/ & ...

Save the promise object from the $http GET request in a local storage

Struggling to wrap my head around how to update data on a graph without the proper Angular service knowledge. All I want is to retrieve a JSON object with one GET request and save it locally on my controller. This way, I can use the original JSON to displa ...

If the variable is empty, use jQuery ajax to send a request with different data

Below is the code I have written: $(document).ready(function() { cookieanimalid =($.cookie("cookieanimal")); $.ajax({ type: 'POST', url: "/myurl/", data: {cookieanimalid}, success: function ( ...

Instead of loading the HTML into the div, Ajax is now sending me the link instead

I have just begun working on a new laravel project and am currently working on the user profile page, which will include a sidebar with links like Portfolio, Account Settings, etc. My goal is to dynamically load each page into a div when a link in the side ...

The code functions properly when tested on a local server, but doesn't produce any results when

Currently, I am working on customizing a premade website and have been tasked with making a modification. The goal is to implement a confirmation box that appears when a button to delete a meeting task is clicked, rather than immediately deleting it. Afte ...

Invoking a function passed via props that utilizes react-router's features

I'm really struggling to grasp this problem. Is there anyone here who could help me out? I have a component where I pass a method called this.fetchContent as props named Filter. This method triggers an action creator that uses axios with Redux to fetc ...

Error in Vue Google Maps: Marker not defined

I'm currently working on integrating a single location map using Google Maps in Vue 2 with Vue-google-maps-2. Despite using code that has successfully worked for other parts of the application where multiple markers are plotted from an array, I am enc ...

Using jQuery AJAX to send POST requests in CodeIgniter

I've been trying to configure the jQuery Ajax post URL, but it's not functioning as expected. I've searched extensively and found many solutions, but none of them seem to solve my issue. I've set the base URL in var baseurl = "<?php ...

Elevation in design ui component

I am having an issue with the height of a theme-ui component I embedded. Even though the console shows it correctly, it is displaying at 100% height. <Embed src={url} sx={{width: '800px', height: '400px'}}/> This embed is contain ...

What is the best way to showcase a chart using jquery?

Is there a way to incorporate trendlines or target lines in highcharts similar to fusion chart? I have been able to draw them successfully in fusion charts. Check out this fiddle link: http://jsfiddle.net/Tu57h/139/ I attempted to recreate the same in hi ...