【发布时间】:2019-10-05 00:41:10
【问题描述】:
我已经使用 Vue 几个月了,我现在正在尝试将 Vuex 与我的应用程序集成,但是当 Vuex 商店发生变化时我无法更新状态。
我创建了一个新应用并实现了一个简单的计数,只是为了看看它是否有效,但当 store.count 更改时,它仍然不会更新 count。
我的测试代码如下。
index.html:
<div id="app">
<template>
<h1>{{count}}</h1>
<button @click="add">Add 1</button>
</template>
</div>
index.js:
import Vue from 'vue/dist/vue.js';
import store from './store';
new Vue({
el: '#app',
store,
data: {},
computed: {
count: () => store.state.count
},
methods: {
add: () => store.commit('add')
}
});
store.js:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
count: 0
},
mutations: {
add: (state) => state.count++
}
});
我知道store.count 正在更新,因为我可以在 Vue 开发工具中看到它。我只是想不通为什么count 值也不会更新,我错过了什么?
【问题讨论】:
标签: javascript vuejs2 vuex