【发布时间】:2019-10-05 01:11:46
【问题描述】:
在以下代码中,组件(app.vue)c2 属性在increment 更新商店计数器this.$store.state.counter++; 时不会得到更新
我知道我可以通过使用 c2 作为 computed 属性来解决这个问题,但我想知道为什么 Vuex 或 vue 不启动反应性,因为计数器的值是通过增量方法更新的。
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>
谢谢
【问题讨论】: