Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How delete an item using vuex?

I just started learning vuex and can`t delete item. I can delete item directly in the component.

deleteCar (cars, id) {
        this.$http.delete('http://localhost:3000/cars/' + cars.id)
          .then(() => {              
              this.cars.splice(id, 1)
          })
      }

In vuex i have:

state: {
    car: {},
    cars: []
  },
  mutations: {
    ADD_CAR (state, car) {
      state.car = car
    },
    GET_CARS (state, cars) {
      state.cars = cars
    }

  },
  actions: {
    createCar({commit}, car) {
      axios.post('http://localhost:3000/cars', car)
        .then(() => {
          commit('ADD_CAR', car)
        })
    },

    loadCars({commit}) {
      axios.get('http://localhost:3000/cars')
        .then(res => {
            const cars = res.data
            commit('GET_CARS', cars)
        })
    }
  }

Code in component where i wanna delete item:

<div class="card mb-3" v-for="(car, i) in cars" :key="i">
      <div class="card-header">
      Cars name: {{ car.carName }}
      </div>
      <div class="card-body">
        <h5 class="card-title">Country: {{ car.country }}</h5>
        <p class="card-text">Year of manufacture: {{ car.carYear }}</p>
        <button class="btn btn-primary mb-5" @click="deleteCar(car, i)">Delete Car</button>
      </div>
    </div>

I can add car and get cars. But can`t delete

like image 758
Николай Гаврилов Avatar asked Dec 01 '18 13:12

Николай Гаврилов


People also ask

What is mapState Vuex?

Mapping State Vuex provides a helper function called mapState to solve this problem. It is used for mapping state properties in the store to computed properties in our components. import { mapState } from 'vuex' 2. export default{

What is strict mode in Vuex?

In strict mode, whenever Vuex state is mutated outside of mutation handlers, an error will be thrown. This ensures that all state mutations can be explicitly tracked by debugging tools.

How do I update my Vuex data?

To create new data, you can use the insert , create , and new methods. They all insert new records to the Vuex Store but behave in a slightly different manner. The insert method will simply insert new records. You should pass an object containing records in data key to the method.


3 Answers

You want to commit a mutation to delete the car

Here is your method

deleteCar (cars, id) {
    this.$http.delete('http://localhost:3000/cars/' + cars.id)
        .then(() => {              
              this.cars.splice(id, 1);
        });
}

Instead of deleteCar(cars, id) you will want to change it to deleteCars({commit}, id)

So your action would be

deleteCar ({commit}, id) {
    this.$http.delete('http://localhost:3000/cars/' + id)
        .then(() => {              
             commit('DELETE_CAR', id);
        });
}

And you have a mutation DELETE_CAR

DELETE_CAR(state, id){
    index = state.cars.findIndex(car => car.id == id);
    state.cars.splice(index, 1);
}
like image 54
TJ Weems Avatar answered Oct 17 '22 06:10

TJ Weems


A simplified answer for clarity.

in the template:

<button @click="deleteCar(car)">Delete Car</button>

method in the component:

 deleteCar(car) {
    this.$store.commit('DELETE_CAR', car);
 }

mutation in the store:

 DELETE_CAR(state, car) {
    var index = state.cars.findIndex(c => c.id == car.id);
    state.cars.splice(index, 1);
 }
like image 6
Andrew Avatar answered Oct 17 '22 05:10

Andrew


I see that you're using Axios with Vue, so your request .delete requesst that delete already but in .then you should do something not related to delete or splice,

     deleteCar (cars) {
        this.$http
          .delete('http://localhost:3000/cars/' + cars.id', { data: payload })
          .then(
            //here write what you want after delete
            res => console.log(res);
          )
      }

in .then you do what you need after delete because of .delete request already deleted that part from your JSON data

like image 1
Moumen Soliman Avatar answered Oct 17 '22 05:10

Moumen Soliman