Vuejs:我需要在第三次按下按钮后将按钮重置回名字

Vuejs: I need to reset the button back to the first name after pressing the button the third time

我有这个 vuejs 代码,我想用一个按钮做循环效果。我希望按钮在第三次单击按钮后立即返回到第一条评论(即在 Javascript 中)。下面是我的 HTML 和 Javascript 代码。非常感谢提前。

HTML:

<button class="next" @click="increment"> > </button>

Javascript:

<script>
export default{
  data(){
    return{
    testimonialData: [
      {
        name: 'W',
        comments: 'It was great.',
        stars: 5
      },
      {
        name: 'Tom',
        comments: 'Easy to use.',
        stars: 4
      },
      {
        name: 'Has',
        comments: 'Test',
        stars: 3
      }
    ],
    number: 0
  }
  },
  methods:{
    increment: function(){
      this.number ++;
    },
  },
}
</script>

希望对您有所帮助!!

new Vue({
  el: "#app",
  data(){
    return{
    testimonialData: [
      {
        name: 'W',
        comments: 'It was great.',
        stars: 5
      },
      {
        name: 'Tom',
        comments: 'Easy to use.',
        stars: 4
      },
      {
        name: 'Has',
        comments: 'Test',
        stars: 3
      }
    ],
    counter: 0
  }
  },
  methods:{
  increment(){
   if((this.testimonialData.length - 1) === this.counter) {
    this.counter = 0;
   }
   else {
    this.counter++;
   }
  }
  },
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<div id="app">
  <h3>{{testimonialData[counter].comments}}</h3>
  <hr>
  <button @click="increment">Increment</button>
</div>

看看这个。

new Vue({
  el: "#app",
  data() {
    return {
      testimonialData: [{
          name: 'W',
          comments: 'It was great.',
          stars: 5
        },
        {
          name: 'Tom',
          comments: 'Easy to use.',
          stars: 4
        },
        {
          name: 'Has',
          comments: 'Test',
          stars: 3
        }
      ],
      counter: 0
    }
  },
  methods: {
    increment() {
      //for generic if(this.counter === this.testimonialData.length - 1)
      if (this.counter == 2)
        this.counter = 0;
      else
        this.counter++;

    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
  <button class="next" @click="increment"> > </button>
  <h3>{{testimonialData[counter].name}}</h3>
  <h4>"{{testimonialData[counter].comments}}" with
    <span>{{testimonialData[counter].stars}} star/s.</span>
  </h4>
</div>