In the following code, components (app.vue) c2 property does not get updated when increment updates stores counter this.$store.state.counter++;
I know I can solve this by using c2 as computed property but I would like to know why Vuex or vue does not initiate reactivity since the counter's value was updated by increment method.
Store.js
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
counter: 0
},
mutation: {
increment() {
return this.$state.counter++;
}
}
});
App.vue
<template>
<div id="app">
<button @click="increment">Increment</button>
{{ c2 }}
</div>
</template>
<script>
export default {
data() {
return {
c2: this.$store.state.counter
};
},
methods: {
increment() {
this.$store.state.counter++;
}
}
};
</script>
Thanks