在开发动态FAQ页面时遇到了这种奇怪的行为,在单击问题后显示答案。
当控制答案打开/关闭的布尔值存储在对象中时,视图会立即更新,并立即显示或隐藏答案。但是,当它存储在列表中时,在以其他方式更新视图之前,不会显示或隐藏答案。
这是显示问题https://jsfiddle.net/masterofginseng/ffqt9n4y/6/
的小提琴HTML
[13:42:17][MSBuild output] SqlBuild:
[13:42:17][MSBuild output] Creating a model to represent the project...
[13:42:17][MSBuild output] Done Building Project "D:\BuildAgent\work\9d2a77191c8abcc1\Focal\Focal.sqlproj" (default targets) -- FAILED.
和javascript:
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="faq">
<div>This responds immediately</div>
<hr>
<div v-for="questionObj in questions1">
<div style="cursor: pointer;" @click="questionObj.open = !questionObj.open">
{{ questionObj.question }}
</div>
<div style="color: blue;" v-show="questionObj.open">{{ questionObj.answer }}</div>
</div>
<br>
<br>
<div>This doesn't respond until the view is updated in some other way (ex. by clicking on one of the questions above)</div>
<hr>
<div v-for="questionarray in questions2">
<div style="cursor: pointer;" @click="questionarray[2] = !questionarray[2]">
{{ questionarray[0] }}
</div>
<div style="color: blue;" v-show="questionarray[2]">{{ questionarray[1] }}</div>
</div>
</div>
答案 0 :(得分:3)
由于Javascript的限制,Vue无法使用以下语法检测项目中值的更改:
questionarray[2] = !questionarray[2]
见这里:https://vuejs.org/v2/guide/list.html#Array-Change-Detection
如上面的链接所述,您必须改为使用splice()
:
questionarray.splice(2, 1, !questionarray[2])
答案 1 :(得分:0)
试试这样:
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="faq">
<div>This responds immediately</div>
<hr>
<div v-for="questionObj in questions1">
<div style="cursor: pointer;" @click="questionObj.open = !questionObj.open">
{{ questionObj.question }}
</div>
<div style="color: blue;" v-show="questionObj.open">{{ questionObj.answer }}</div>
</div>
<br>
<br>
<div>This doesn't respond until the view is updated in some other way (ex. by clicking on one of the questions above)</div>
<hr>
<div v-for="questionarray in questions2">
<div style="cursor: pointer;" @click="myMethod(questionarray)">
{{ questionarray[0] }}
</div>
<div style="color: blue;" v-show="questionarray[2]">{{ questionarray[1] }}</div>
</div>
</div>
脚本
new Vue({
el: "#faq",
data: {
questions1: [{
question: "How big is it?",
answer: "very big",
open: false
}, {
question: "How small is it?",
answer: "very small",
open: false
}],
questions2: [
["How big is it?", "very big", false],
["How small is it?", "very small", false]
]
},
methods:{
myMethod(questionArray){
this.$set(questionArray, 2, !questionArray[2]);
}
}
});
以下是jsFiddle
或者像Linus博格回答的一个不那么冗长的版本:
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="faq">
<div>This responds immediately</div>
<hr>
<div v-for="questionObj in questions1">
<div style="cursor: pointer;" @click="questionObj.open = !questionObj.open">
{{ questionObj.question }}
</div>
<div style="color: blue;" v-show="questionObj.open">{{ questionObj.answer }}</div>
</div>
<br>
<br>
<div>This doesn't respond until the view is updated in some other way (ex. by clicking on one of the questions above)</div>
<hr>
<div v-for="questionarray in questions2">
<div style="cursor: pointer;" @click="questionarray.splice(2, 1, !questionarray[2])">
{{ questionarray[0] }}
</div>
<div style="color: blue;" v-show="questionarray[2]">{{ questionarray[1] }}</div>
</div>
</div>