我正在使用Vue。我为我的测试用例创建了MultiAccordion
组件。我想用status[index]
旗打开幻灯片。但是这个组件不起作用。为什么这不起作用?
var MultiAccordion = {
data: function() {
return {
items: [
'cat',
'dog',
'bird',
],
status: [],
};
},
created: function() {
for (let i = 0; i < this.items.length; ++i) {
this.status.push(false);
}
},
methods: {
handleToggle: function(index) {
this.status[index] = !this.status[index];
},
},
template: `
<ul>
<li v-for="(val, index) in items">
<button @click="handleToggle(index)">toggle</button>
<div v-if="status[index]">
{{ val }}
</div>
</li>
</ul>
`,
};
this.status[index] = !this.status[index];
你要么必须使用Vue.set
:
this.$set(this.status, index, !this.status[index])
或者使用splice
:
this.status.splice(index, 1, !this.status[index])