简体   繁体   English

如何遍历VueJS中的项目列表

[英]How to traverse a list of items in VueJS

Here is my vue instance: 这是我的Vue实例:

new Vue({
    el: '#app',
    data: {
      showPerson: true,
      persons:
        [
          {id: 1, name: 'Alex'},
          {id: 2, name: 'Bob'},
          {id: 3, name: 'Chris'}
        ],
    },
    methods: {
      nextPerson: function(){
        this.showPerson = false;
      }
    }
  });

I am trying to walk the persons array of objects. 我正试图走动persons的对象数组。 I want the list to start with the first element of the array and below it should be a button which is responsible for hiding the previous element and showing the next element of the array. 我希望列表从数组的第一个元素开始,在它的下面应该是一个按钮,它负责隐藏数组的前一个元素并显示数组的下一个元素。 Once the user reaches the last element, the Next button should not go back to the first element. 一旦用户到达最后一个元素,则“下一步”按钮不应返回到第一个元素。

Here is the HTML: 这是HTML:

<div id="app">
  <ul v-for="person in persons">
    <li v-if="showPerson">{{person.name}}</li>
  </ul>
  <button @click="nextPerson">Next Person</button>
</div>

And the JSBin Link. 还有JSBin Link。 At this moment I can only show and hide the items all at once and not one at a time. 目前,我只能一次显示和隐藏所有项目,而一次只能隐藏一次。 How can I implement this? 我该如何实施?

One of the ways of doing so would be to keep an index for the person being shown on screen. 这样做的方法之一是保持屏幕上显示的人的索引。 I've named this variable as shownPersonIndex . 我已将此变量命名为shownPersonIndex

Then, you need to show the next person on click of button. 然后,您需要在单击按钮时显示下一个人。 So in the click event handler, you need to increment the index by 1. Also, you need to ensure that the index value does not exceed the length of the array. 因此,在click事件处理程序中,您需要将索引增加1。此外,还需要确保索引值不超过数组的长度。 So I've modified the click handler as follows: 因此,我对点击处理程序进行了如下修改:

nextPerson: function() {
  if(this.shownPersonIndex < (this.persons.length - 1)) {
    this.shownPersonIndex++;
  }
}

Finally, you can either use a computed to display the currently shown person or an inline expression like this.persons[this.shownPersonIndex].name to show the person on screen. 最后,您可以使用计算来显示当前显示的人,也可以使用诸如this.persons[this.shownPersonIndex].name的内联表达式在屏幕上显示该人。

I am using v-if="this.shownPersonIndex != this.persons.length - 1" to hide the "next" button as you reach the last element on the array. 我使用v-if="this.shownPersonIndex != this.persons.length - 1"隐藏“下一个”按钮,直到您到达数组的最后一个元素。

 new Vue({ el: '#app', data: { shownPersonIndex: 0, persons: [{ id: 1, name: 'Alex' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Chris' } ], }, methods: { nextPerson: function() { if(this.shownPersonIndex < (this.persons.length - 1)) { this.shownPersonIndex++; } } }, computed: { shownPerson: function() { return this.persons[this.shownPersonIndex]; } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script> <div id="app"> Person: {{ shownPerson.name }} <button v-if="this.shownPersonIndex != this.persons.length - 1" @click="nextPerson">Next Person</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM