vuex 状态树的使用

 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。


交互式爱情
浏览 830回答 2
2回答

婷婷同学_

不是$store.state.wishNum吗?

慕妹3242003

state 和 mutations 放在一个文件中吗?你可以分开写 state.jsexport&nbsp;default&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;wisNum:&nbsp;0}mutations.jsexport&nbsp;default&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;SET_WISH:&nbsp;(state,&nbsp;payload)&nbsp;=>&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state.wishNum&nbsp;=&nbsp;payload &nbsp;&nbsp;&nbsp;&nbsp;} }index.js中import&nbsp;state&nbsp;from&nbsp;'./state.js' import&nbsp;mutations&nbsp;from&nbsp;'./mutations.js' export&nbsp;default&nbsp;new&nbsp;Vuex.Store({&nbsp;&nbsp;&nbsp;&nbsp;state,&nbsp;mutations })Vue组件中this.$store.commit('SET_WISH',&nbsp;12)main.js中import&nbsp;store&nbsp;from&nbsp;'./store/index.js'new&nbsp;Vue({ &nbsp;&nbsp;el:&nbsp;'#app', &nbsp;&nbsp;router, &nbsp;&nbsp;components:&nbsp;{&nbsp;App&nbsp;},&nbsp;&nbsp;template:&nbsp;'<App/>', &nbsp;&nbsp;store })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Vue.js