如何解决VUEX中的状态更改?

时间:2019-05-30 06:25:36

标签: javascript vue.js vuejs2 vuex store

在从另一个数组拼接元素然后更改状态时更改状态时遇到问题。需要明确的是,我不想改变状态,只想从数组arrayWithFilters = []中剪接一个元素

export const SHOW_RESULTS_WHEN_NOTHING = (state) => {
  let selectedFilters = {...state.selected.filters},
  arrayWithFilters = []; //Making splice from this array, but it also removes from state.selected.filters. 

  for (let filter in selectedFilters) {
    arrayWithFilters.push(selectedFilters[filter])
  }

  arrayWithFilters.forEach((filter) => {
    if (filter.values.includes('qcontrast')) {
      filter.values.splice('qcolor', 1);
      console.log(filter)
    }
  })
}

1 个答案:

答案 0 :(得分:1)

下一个代码解决了问题

export const SHOW_RESULTS_WHEN_NOTHING = (state) => {
let selectedFilters = {...state.selected.filters},
    arrayWithFilters = [];

let selectedFiltersCopy = JSON.parse(JSON.stringify(selectedFilters));

arrayWithFilters = Object.values(selectedFiltersCopy);

arrayWithFilters.forEach((filter) => {

    if (filter.values.includes('qcontrast')) {

        filter.values.splice('qcontrast', 1);

    }
})

console.log(arrayWithFilters)

}