我有一个Numbers数组,我正在使用该.push()方法向其中添加元素.
有没有一种简单的方法从数组中删除特定元素?相当于像array.remove(number);.
我必须使用核心的JavaScript - 无框架是不允许的.
我创建了一个包含三个元素的组件"my-item":一个下拉列表(由"itemList"填充)和从下拉列表中填充的两个输入框.该组件被认为是一行.
我试图一次添加和删除一行,但我不确定两件事.(1)要添加到行数组中的内容?(2)为什么this.rows.splice(index,1)只删除最后一行?
https://jsbin.com/mugunum/edit?html,output
谢谢
<div id="app">
    <my-item v-for="(row, index) in rows"
         :itemdata="itemList"
          v-on:remove="removeRow(index)">
    </my-item>
<div>
    <button @click="addRow"> Add Row </button>
</div>
</div>
<template id="item-template">
<div>
    <select v-model="selected">
        <option v-for="item in itemdata"  :value="item">
           {{ item.code }}
        </option>
    </select>
    <input type="text" placeholder="Text" v-model="selected.description">
    <input type="text" placeholder="value" v-model="selected.unitprice">
    <button v-on:click= "remove"> X </button>
</div>
</template>
Vue.component('my-item', {
props: ['itemdata'],
template: '#item-template',
data: function () {
    return {
    selected: this.itemdata[0]
    }
},
methods: {
    remove() {
        this.$emit('remove');
    }
}
}),
new Vue({
el: "#app", …刚开始学习vuex,不能删除item。我可以直接在组件中删除项目。
deleteCar (cars, id) {
        this.$http.delete('http://localhost:3000/cars/' + cars.id)
          .then(() => {              
              this.cars.splice(id, 1)
          })
      }
在 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)
        })
    }
  }
我想删除项目的组件中的代码:
<div class="card mb-3" v-for="(car, i) in …我有基本的v-循环播放array:
<template v-for="(item, index) in array">
    {{item.text}}
    <btn @click="delete">Delete me!</btn>
</temaplate>
我希望能够删除循环中的项目。我该怎么做?我可以简单地使用splice()或删除没有要删除元素的索引吗?