鍍金池/ 問答/HTML/ vue v-for 渲染如何綁定數(shù)據(jù)

vue v-for 渲染如何綁定數(shù)據(jù)

各位大佬好,問題如下:
我用v-for 渲染了一個(gè)列表,列表內(nèi)有4個(gè)子元素,每次只顯示一個(gè)
data中有一個(gè)currentIndex, 請問怎么用currentIndex控制元素的顯示和隱藏呢。。代碼如下

<div class="pt-page page-2" :class="pageCurrent">
      <div class="pt-answer-box">
        <div class="pt-answer-index">
        <div v-if="questionData">
            <div  v-for="(value, key) in questionData" class='plugin-answer'>
              <div class="header-progress">
                進(jìn)度:<span class="answer-current-index">{{currentAnIndex}}</span>/<span class="answer-all-count">5</span>
              </div>
              <div class="answer-header">
                <div class="question-img">
                  <img src="../../assets/img/question_1.png" alt="">
                </div>
                <div class="question-title">
                <span >{{value.question}} </span>
                </div>
              </div>
              <div class="answer-text-list" v-for="seleItem in value.select">
                <div class="item-text" @touchstart="usrTouch(seleItem, $event)">
                  <span class="span-value" >{{seleItem}}</span>
                </div>
              </div>
            </div>
        </div>
        </div>
      </div>
    </div>
<script>
    export default {
    name: 'question',
        props: {
            questionData: {
                type: [Object, Array],
            },
        },
        data() {
            return {
                currentAnIndex: 1,//當(dāng)前問題索引
                anStatus: {
                  'item_right': false,
                  'item_error': false,
                },
                answered: false,// 是否答題
                
              }
        },
    computed: {
      
    },
    methods: {
      usrTouch(seleItem,e) {
        if (this.questionData[this.currentAnIndex].answer == seleItem) {
          //回答正確
          e.srcElement.className += ' item_right';
        } else {
          //回答正確
          e.srcElement.className += ' item_error';
        }
        
      }
    }
    }
</script>

謝謝各位

回答
編輯回答
櫻花霓

currentIndex控制顯隱的邏輯是什么呢?

另外,

e.srcElement.className += ' item_right'

用vue的話,就盡量不要這樣寫了,試試數(shù)據(jù)綁定class

2018年2月4日 15:34
編輯回答
冷溫柔

v-if="key==curentindex"

2017年9月25日 04:23
編輯回答
傲嬌范

v-for的時(shí)候你可以獲取到索引index,例如題中如果數(shù)組長度是4,則index一次是0,1,2,3,那么你可以定義一個(gè)變量,例如current:0,在v-for后面跟v-if="index == current",那么默認(rèn)只會顯示第1條數(shù)據(jù),你去改變這個(gè)current(0-3,超出就顯示為空了),列表就變了。

2018年8月27日 03:09
編輯回答
乖乖瀦

感謝各位 問題解決了

2018年9月3日 05:50