gpt4 book ai didi

javascript - Vuex - 基于模块状态的计算属性不会在调度时更新?

转载 作者:行者123 更新时间:2023-11-30 14:17:17 25 4
gpt4 key购买 nike

我正在探索 Vuex 的模块文档,现在已经停滞了很长一段时间,无法从使用它的组件更新模块状态中的值。这是我目前所拥有的:

app/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import Counter from './modules/Counter'
Vue.use(Vuex)
export default new Vuex.Store({
modules: { Counter }
})

app/store/modules/Counter.js

const state = {
current: 0
}

const mutations = {
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
}

const actions = {
increment ({ commit }) {
commit('INCREMENT_CURRENT_COUNT')
}
}

export default {
namespaced: true,
state,
mutations,
actions
}

app/components/Test.vue

<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>

基本上,我要做的就是在单击组件中触发 add() 的按钮时增加 Counter 模块中的 current 方法,这正是我所期望的,但实际上并没有发生什么。

所以没有错误,vue 组件中的 count 值保持为 0。

关于我在这里做错了什么有什么想法吗?

最佳答案

您应该将 state.main++ 更改为 state.current++

const mutations = {
INCREMENT_CURRENT_COUNT (state) {
// change state.main -> state.current
state.current++
}
}

关于javascript - Vuex - 基于模块状态的计算属性不会在调度时更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com