Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update state in vuex ? vue.js 2

My vue component is like this :

<template>
    <div>
        <div class="row">
            <div class="col-sm-3">
                <div class="form-group">
                    <label for="status" class="sr-only">Status</label>
                    <select class="form-control" v-model="selected" @change="filterByStatus()">
                        <option value="">All Status</option>
                        <option v-for="status in orderStatus" v-bind:value="status.id">{{ status.name }}</option>
                    </select>
                </div>
            </div>
        </div>
        ...
    </div>
</template>

<script>
    import { mapActions, mapGetters } from 'vuex';
    export default {
        ...
        data() {
            return { selected: '' }
        },
        methods: {
            filterByStatus: function() {
                this.$store.state.status = this.selected
            }
        }
    }
</script>

My modules order vuex is like this :

import { set } from 'vue'
import order from '../../api/order'
import * as types from '../mutation-types'

const state = {
    status: ''
}
const getters = {
    ...
}
const actions = {
    ...
}

const mutations = {
    ...
}

export default {
    state,
    getters,
    actions,
    mutations
}

I use this : this.$store.state.order.status = this.selected, to update state when executed, but there exist error like this :

[Vue warn]: Error in callback for watcher "function () { return this._data.$$state }": (found in )

Error: [vuex] Do not mutate vuex store state outside mutation handlers.

How can I solve it?

I want update state, because I want the value used by component another

like image 297
samuel toh Avatar asked Apr 11 '17 06:04

samuel toh


People also ask

How do I change my Vuex state?

Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations.

Does Vuex work with Vue 2?

Vuex is a library which helps you to enforce a Flux-like application architecture in your Vue. js 2 application.

Does Vuex keep state on refresh?

To persist Vuex state on page refresh, we can use the vuex-persistedstate package. import { Store } from "vuex"; import createPersistedState from "vuex-persistedstate"; import * as Cookies from "js-cookie"; const store = new Store({ // ...


1 Answers

You must have received this error because of enabling strict mode in your vuex store setup.

This, however, is a good practice. You must not modify state except from within a mutation.

So to use the newly setup store; have a mutation in like:

const mutations = {
   mutateOrderStatus: function (state, payload) {
      state.order.status = payload
   }
}

const actions = {
   updateOrderStatusAction: function ({commit}, payload) {
      commit('mutateOrderStatus', payload)
   }
}

Now include it in your component like:

...
methods: {
  ...mapActions([ // spread operator so that other methods can still be added.
    'updateOrderStatusAction'
  ]),
  filterByStatus: function() {
    this.updateOrderStatusAction(this.selected)
  }
}

Note: you might need babel and babel-preset-es2015 installed to make use of spread operator: ....

like image 59
Amresh Venugopal Avatar answered Oct 03 '22 04:10

Amresh Venugopal