Javascript Vuex-基于模块状态的计算属性在分派时不更新?
我正在研究Vuex的模块文档,并且已经被困了很长一段时间,无法从使用它的组件更新模块状态中的值。以下是我到目前为止的情况: app/store/index.js app/store/modules/Counter.js app/components/Test.vueJavascript Vuex-基于模块状态的计算属性在分派时不更新?,javascript,vuejs2,vuex,Javascript,Vuejs2,Vuex,我正在研究Vuex的模块文档,并且已经被困了很长一段时间,无法从使用它的组件更新模块状态中的值。以下是我到目前为止的情况: app/store/index.js app/store/modules/Counter.js app/components/Test.vue {{count}} 做点什么 累加 从“vuex”导入{mapState,mapActions} 导出默认值{ 计算:mapState({ 计数:state=>state.Counter.current }), 方法:{ 某物()
{{count}}
做点什么
累加
从“vuex”导入{mapState,mapActions}
导出默认值{
计算:mapState({
计数:state=>state.Counter.current
}),
方法:{
某物(){
警报(“你好”)
},
…映射操作('计数器'{
添加:“增量”
}),
}
}
基本上,我所要做的就是在计数器
模块中增加当前
值,当单击触发add()
方法的组件中的按钮时,这是我在上述情况下所期望的,但实际发生的是什么都没有
因此,没有错误,并且vue组件中的count
值保持为0
你知道我做错了什么吗?问题不在于单击处理程序或操作,而在于变异。 在您的突变
增量\u当前\u计数中
有以下内容:
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
而您所在的州只有一个名为current
的属性
为了使其工作,请更改:
state.main++
致:
您应该将
state.main++
更改为state.current++
const突变={
增量\当前\计数(状态){
//更改state.main->state.current
状态电流++
}
}
您能否在Codesandbox上复制此问题?这将使调试和理解变得更容易。@Phiter感谢您的输入。而且它很有效(facepalm)。我的用例和问题稍微复杂了一点,正如我在对答案的评论中所解释的。该死的,我在阅读问题时甚至没有注意到这一点,我没有注意到这一点,谢谢!这是说我在codesandbox上做示例时发现的。我的问题是,我将它与electron结合使用,特别是vuex-electron
,没有注意到它说的您需要在主进程中创建存储实例的部分。解决了我的问题,虽然我知道它并没有包含在我的简单示例中(这个包还没有标签)。
<template>
<div id="wrapper">
<p>{{ count }}</p>
<button @click="something()">Do something</button>
<button @click="add()">Add to count</button>
</div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
export default {
computed: mapState({
count: state => state.Counter.current
}),
methods: {
something () {
alert('hello')
},
...mapActions('Counter', {
add: 'increment'
}),
}
}
</script>
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
state.main++
state.current++