Reputation: 7076
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
Upvotes: 5
Views: 20206
Reputation: 51
I just found a solution of that problem, i used
Vue.set(state.element, elementIndex, newElement);
Upvotes: 0
Reputation: 9549
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: ...
.
Upvotes: 11