Watching array stored in Vuex in VueJS
First and foremost, be careful when using .sync
: it will be deprecated in 2.0.
Take a look at this: http://vuex.vuejs.org/en/forms.html, as this problem is solved in here. Basically, this checkbox should trigger a vuex
action on input
or change
. Taken from the docs:
<input :value="message" @input="updateMessage">
Where updateMessage
is:
vuex: { getters: { message: state => state.obj.message }, actions: { updateMessage: ({ dispatch }, e) => { dispatch('UPDATE_MESSAGE', e.target.value) } }}
If you do not wish to track the mutations, you can move the state of this component away from vuex
, to be able to use v-model
in all its glory.
You just have to make a custom getter and setter:
<template> <ui-checkbox :value.sync="thisCustomer"></ui-checkbox></template><script> //this is using vuex 2.0 syntax export default { thisCustomer: { get() { return this.$store.state.customer; }, set(val) { this.$store.commit('SET_CUSTOMER', val); // instead of performing the mutation here, // you could also use an action: // this.$store.disptach('updateCustomer') } }, }</script>
In your store:
import { SET_CUSTOMER,} from '../mutation-types';const state = { customer: null,};const mutations = { [SET_CUSTOMER](state, value) { state.customer = value; },}
I'm not exactly sure what your store looks like, but hopefully this gives you the idea :)