鍍金池/ 問答/HTML/ vuex 狀態(tài)樹的使用

vuex 狀態(tài)樹的使用

 export const state = () => ({
   wishNum: 0
  });
 export const mutations = {
  SET_WISH: (state) => {
     state.wishNum = 0;
 },
}

 let Num = res.total;
 setStore("wishNum", Num);

##
我這樣寫對不對。我獲取不到$store.state.user.wishNum的值本地的值在變化然而他還是0。

回答
編輯回答
入她眼

state 和 mutations 放在一個文件中嗎?
你可以分開寫 state.js

export default {
    wisNum: 0
}

mutations.js

export default {
    SET_WISH: (state, payload) => {
        state.wishNum = payload
    }
}

index.js中

import state from './state.js'
import mutations from './mutations.js'
export default new Vuex.Store({
    state, mutations
})

Vue組件中

this.$store.commit('SET_WISH', 12)

main.js中

import store from './store/index.js'
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store
})
2017年12月24日 14:36
編輯回答
怪痞

不是$store.state.wishNum嗎?

2017年8月11日 16:10