如何定位和擦除 Vue 中的多组件

How to target and erase a multiplicated component in Vue

我需要定位并擦除我使用 v-for 复制的组件。 我正在创建多个 stopWatch 应用程序。 此时我只能擦除我复制的最后一个组件,但我希望能够擦除任何目标组件 这是我的组件“计数器”:

<template>
  <div class="chrono">
    <h2><input type="text" :placeholder="'Chrono' + number" /></h2>
    <div class="timer">
      <input type="number" v-model.number="hours">:
      <input type="number" v-model.number="minutes">:
      <input type="number" v-model.number="seconds">
    </div>
    <div class="controls">
      <button @click="handleCounter">{{ startStop }}</button>
      <button @click="resetCounter">reset</button>
    </div>
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "Counter",
  data() {
    return {
      hours: 0,
      minutes: 0,
      seconds: 0,
      startStop: "start",
      interval: "",
    };
  },
props:["number"],
  methods: {
    handleCounter() {
      if (this.startStop === "start") {
        this.interval = setInterval(
          function () {
            this.seconds++;
            if (this.seconds + 1 > 60) {
              this.minutes++;
              this.seconds = 0;
            }
            if (this.minutes + 1 > 60) {
              this.hours++;
              this.seconds = 0;
              this.minutes = 0;
            }
          }.bind(this),
          1000
        );
        this.startStop = "stop";
      } else if (this.startStop === "stop") {
        clearInterval(this.interval);
        this.startStop = "start";
      }
    },
    resetCounter() {
      this.seconds = 0;
      this.minutes = 0;
      this.hours = 0;
    },
  },
};
</script>

<style scoped lang="scss">
.chrono {
  border: 1px solid black;
  margin: auto;
  border-radius: 5px;
}
.timer{
display: flex;
flex-flow: row;
justify-content: center;

}
.timer input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  
  -webkit-appearance: none;
  margin: 0;
}
.timer input{
  width: 25px;
  border: none;
  text-align: center;
}

</style>

还有 App.vue 我想复制或删除我的计数器组件的地方:

<template>
  <section>
    <button @click="addCounter">+</button>
    <div class="listOfCounter" >
      <Counter v-for="index in count" :key="index" :number="index">
        <button @click="removeCounter">-</button>
      </Counter>
    </div>
  </section>
</template>

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

export default {
  name: "App",
  components: {Counter},
  data() {
    return { count: 1,
    index:[]
    };
  },
  methods: {
    addCounter() {

      this.count++;
    },
    removeCounter() {
      this.count--;
    },
  },
};
</script>

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

您可以将 count 转换为数组,然后将 push/filter 转换为 add/remove 计数器:

Vue.component('Counter', {
  template: `
    <div class="chrono">
    <h2><input type="text" :placeholder="'Chrono' + number" /></h2>
    <div class="timer">
      <input type="number" v-model.number="hours">:
      <input type="number" v-model.number="minutes">:
      <input type="number" v-model.number="seconds">
    </div>
    <div class="controls">
      <button @click="handleCounter">{{ startStop }}</button>
      <button @click="resetCounter">reset</button>
    </div>
    <slot></slot>
  </div>
  `,
  data() {
    return {
      hours: 0,
      minutes: 0,
      seconds: 0,
      startStop: "start",
      interval: "",
    };
  },
  props:["number"],
  methods: {
    handleCounter() {
      if (this.startStop === "start") {
        this.interval = setInterval(
          function () {
            this.seconds++;
            if (this.seconds + 1 > 60) {
              this.minutes++;
              this.seconds = 0;
            }
            if (this.minutes + 1 > 60) {
              this.hours++;
              this.seconds = 0;
              this.minutes = 0;
            }
          }.bind(this),
          1000
        );
        this.startStop = "stop";
      } else if (this.startStop === "stop") {
        clearInterval(this.interval);
        this.startStop = "start";
      }
    },
    resetCounter() {
      this.seconds = 0;
      this.minutes = 0;
      this.hours = 0;
    },
  },
})

new Vue({
  el: '#demo',
  data() {
    return { 
      count: [0],
      index: []
    };
  },
  methods: {
    addCounter() {
      this.count.push(Math.max(...this.count)+1);
    },
    removeCounter(index) {
    console.log(this.count)
      this.count = this.count.filter(i => i !== index);
    },
  },
})
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.chrono {
  border: 1px solid black;
  margin: auto;
  border-radius: 5px;
}
.timer{
display: flex;
flex-flow: row;
justify-content: center;

}
.timer input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  
  -webkit-appearance: none;
  margin: 0;
}
.timer input{
  width: 25px;
  border: none;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <section>
    <button @click="addCounter">+</button>
    <div class="listOfCounter" >
      <Counter v-for="index in count" :key="index" :number="index">
        <button @click="removeCounter(index)">-</button>
      </Counter>
    </div>
  </section>
</div>