动态绑定class有时候生效,有时候不生效,是我哪里写的有问题么
<view
v-for="(item, index) in question.answer" :key="index"
class="list-item"
:class="{
'correct': correctIndex === index,
'selected': selectIndex === index
}"
@tap="selectAnswer(item, index)"
>{{item}}</view>
selectAnswer(selectAnswer, selectIndex) { // 选择答案
const { answer: list, info: { fan } } = this.question
const correctIndex = list.findIndex(v => v === fan) // 获取正确答案下标
if (selectAnswer === fan) { // 回答正确
this.correctIndex = correctIndex
this.correct = ++this.correct
} else { // 回答错误
this.correctIndex = correctIndex
this.selectIndex = selectIndex
}
}
咚咚咚咚队长 (作者)
data里面selectIndex默认是空字符串,当我选择第一个的时候,this.selectIndex = 0,但是不会触发class,需要把data里面的selectIndex默认是null才行,不知道为什么
2020-11-03 16:26