state.js:
export default () => ({
stepBarItems: [
{
title: 'General Info',
active: false,
current: false
},
{
title: 'Personal Details',
active: false,
current: false
},
{
title: 'Travel Details',
active: false,
current: false
},
{
title: 'Payment',
active: false,
current: false
},
{
title: 'Upload Documents',
active: false,
current: false
}
]
})
mutations.js:
export default {
setCurrentStepBarItem(state) {
const index = state.stepLevel - 1
state.stepBarItems[index].active = true
state.stepBarItems[index].current = true
}
}
form.vue
created() {
this.$store.commit('visa/setCurrentStepBarItem')
},
问题在于突变没有反应性。
如您所见,状态已更改,我使用getter来获取stepBarItems,但没有任何更改。 问题是什么?
答案 0 :(得分:1)
您确实不需要这些活动/当前变量。 我举例说明了如何使用计算属性来获取所需格式
new Vue({
el: "#app",
data: () => ({
stepBarItems: [
'General Info',
'Personal Details',
'Travel Details',
'Payment',
'Upload Documents'
],
stepLevel: 0
}),
computed: {
computedStepBarItems() {
return this.stepBarItems.map((item, index) => ({
title: item,
current: this.stepLevel === index,
active: this.stepLevel >= index
}))
}
},
methods: {
next() {
this.stepLevel += 1
},
prev() {
this.stepLevel -= 1
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<pre>{{ computedStepBarItems }}</pre>
<button @click="prev()">Prev</button>
<button @click="next()">Next</button>
</div>