Haroon Aslam
Haroon Aslam

Reputation: 1653

Need to sort the vuex Data i need to change computed property in a component result

I need to sort the array which I am receiving after Axios and I need to sort value in ascending and descending also by the date. I don't really know how Vuex works and I need to create the sort function really fast. Can you guys please help me out?

methods: {
  sort() {
    return this.results = this.$store.getters.getResults.slice().reverse();
  },
}
const actions = {
  sort({commit}, payLoad){
    commit('setResults', )
 },
const mutations = {
  setResults (state, results) {
    state.results = results
  },
}

Can you tell me if I am doing correctly? I need to sort the array by its title.

Upvotes: 0

Views: 886

Answers (1)

Vuex Actions are powerful tools for executing asynchronous operations so if there's no reason to keep it in your component then I would dispatch the action to trigger your axios request in your Vuex actions. On the return result in your then function of your axios promise you can then sort your data and finally commit this sorted data to your Vuex store using the setResults mutation.

After this, any computed method in your components or template referencing the $store.state.results variable should update.

Upvotes: 1

Related Questions