Vuex的state问题,{{count}}正常应显示为44,请教为什么会显示为0,是我哪里写错了呢?
发布于 7 年前 作者 skytulip 1833 次浏览 最后一次编辑是 7 年前 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利

上原码: 一、main.js原码: import Vue from ‘vue’ import store from ‘./store’ import vuex from ‘./vuex.vue’

new Vue({ el:’#app’, store, render: xx=>xx(vuex) })

二、store.js原码: import Vue from ‘vue’ import Vuex from ‘vuex’ Vue.use(Vuex)

const state ={ count : 44 } const mutations ={ jia(state){ state.count ++ }, jian(state){ state.count – } }

export default new Vuex.Store({ state, mutations })

三、vuex.vue原码: <template>

Hello Vuex

{{$store.state.count}} - {{count}}

<button @click="$store.commit(‘jia’)">+</button> <button @click="$store.commit(‘jian’)">-</button>

</template>

<script> export default{ name:'app', data(){ return { count:0 } }, computed:{ count(){ return this.$store.state.count } } } </script>
回到顶部