小编Ник*_*лов的帖子

如何使用 vuex 删除项目?

刚开始学习vuex,不能删除item。我可以直接在组件中删除项目。

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

在 vuex 我有:

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)
        })
    }
  }
Run Code Online (Sandbox Code Playgroud)

我想删除项目的组件中的代码:

<div class="card mb-3" v-for="(car, i) in …
Run Code Online (Sandbox Code Playgroud)

vue.js axios vuex

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

axios ×1

vue.js ×1

vuex ×1