【发布时间】:2016-12-16 13:44:46
【问题描述】:
我有一个项目列表,我想为当前选定的项目应用一种样式。我也在使用 Vuex 来管理状态。
我的列表组件:
const List = Vue.component('list', {
template:
'<template v-if="items.length > 0">' +
'<ul class="list-group md-col-12">' +
'<a href="#" v-for="(item, index) in items" class="list-group-item list-group-item-action" v-bind:class="{ active: item.isActive }" v-on:click="selectItem(index);">{{ g.text }}</a>' +
'</ul>' +
'</template>'
computed: {
items: function() {
return this.$store.state.items;
}
},
methods: {
selectItem: function (index) {
this.$store.commit('selectItem', index);
}
}
});
我的商店:
const store = new Vuex.Store({
state: {
items: [],
currentIndex: -1
},
mutations: {
selectItem: function(state, index) {
if (index === state.currentIndex) {
return;
}
if (state.currentIndex > -1) {
delete state.items[state.currentIndex].isActive;
}
state.currentIndex = index;
state.items[state.currentIndex].isActive = true;
}
}
});
我看到,在 Chrome DevTools 中使用 Vue 的“选项卡”时,每当我点击列表中的一个项目时,“项目”数组都会正确更新,但没有在它们上设置类。
此外,使用时间旅行调试来遍历所有突变,在这种情况下,类已设置。
知道为什么会出现这种行为以及如何解决它吗?
【问题讨论】:
标签: javascript vue.js vuejs2