I have a vue store which has the following
store.js
import Vue from 'vue'
import Vuex from 'vuex'
const state = {
supplementStore: {}
}
const actions = {
getDataFromApi ({dispatch, commit}) {
APIrunning.then(response => {
commit('SET_SUPPLEMENT', response)
})
}
}
const mutations = {
SET_SUPPLEMENT (state, data) {
state.supplementStore= data
}
}
const foodstore = {
namespaced: true,
state,
actions,
mutations
}
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
foodstore
}
})
My vue component looks like this
Supp.vue
<template>
<input type="checkbox" v-model="supps.logged">
</template>
<script>
import {mapState, mapActions} from 'vuex'
import store from './store'
export default {
data () {
return {
supps: []
}
},
mounted () {
this.supps = this.supplementStore
},
computed: {
...mapState('foodstore', ['supplementStore'])
}
}
</script>
As you can see I have a component level state called supps
which is assigned the value of supplementStore
(which is a vuex state) as soon as it is mounted
.
mounted () {
this.supps = this.supplementStore
},
supplementStore
gets its value from the the API and it is a JSON object which looks like this
supplementStore = {
logged: true
}
Therefore, when my Supp.vue
component is mounted my local state supps
will become
supps = {
logged: true
}
supps
is binded to an input field of type checkbox (Supp.vue)
using the v-model
directive.
What I want to achieve:
When I toggle the checkbox, supps.logged
should toggle between true
and false
but, supplementStore.logged
should remain unchanged (since I have not binded it to my input field).
What I observe in my
Vue Devtools
:
When I toggle the checkbox, both supps.logged
AND supplementStore.logged
are toggling in sync i.e both of them are toggling in sync between true and false, whereas I want only supps.logged
to get toggled.
Can anyone help me?