Vue
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuex/3.1.0/vuex.min.js"></script>
<div id="app">
<ul>
<li v-for="item in items">
<p>{{item.close ? 'закрыто' : 'открыто'}}</p>
<button @click="handleClick(item)">{{item.close ? 'открыть' : 'закрыть'}}</button>
</li>
</ul>
<button @click="openAll()">открыть все</button>
</div>
Vue
const store = new Vuex.Store({
state: {
items: [{
close: false
}, {
close: true
}, {
close: false
}]
},
mutations: {
toggleState(state, item) {
item.close = !item.close
},
openAll(state) {
state.items.forEach(item => item.close ? item.close = false : null)
}
}
})
new Vue({
el: "#app",
store,
computed: {
items() {
return this.$store.state.items
}
},
methods: {
handleClick(item) {
this.$store.commit('toggleState', item)
},
openAll() {
this.$store.commit('openAll')
}
}
})