Мутация мутации Vuex не реактивна - PullRequest
0 голосов
/ 28 января 2020

состояние. 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
    }
  ]
})

мутации. js:

export default {
  setCurrentStepBarItem(state) {
    const index = state.stepLevel - 1
    state.stepBarItems[index].active = true
    state.stepBarItems[index].current = true
  }
}

форма. vue

  created() {
    this.$store.commit('visa/setCurrentStepBarItem')
  },

проблема в том, что мутация не является реактивной.

enter image description here

enter image description here

Как вы видите, состояние изменилось, и я использую getter для получения stepBarItems, но ничего не изменилось. в чем проблема?

1 Ответ

1 голос
/ 28 января 2020

Вам не нужны эти активные / текущие переменные. Я сделал пример использования вычисляемых свойств для получения желаемого формата

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
    }
}
})


  {{ computedStepBarItems }}
Предыдущая Следующая
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...