vuex中封装moudles

153 阅读1分钟

在store目录下新建目录modules, 新建 catagtory.js和newlist.js

模块结构

export default {
  namespaced: true,
  state: {
    catagtory: [],
    currentCatagtory: ''
}
  mutations: {
   updateCatagtory (state, payload) {
      state.catagtory = payload // 更新分类数据
   },
   updateCurrentCatagtory (state, payload) {
      state.currentCatagtory = payload
   }
  },
  actions: {}
}

在store/index.js中引入定义的两个模块

import catagtory from './modules/catagtory'
import newlist from './modules/newlist'
 export default new Vuex.Store({
  state: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  //引用
    catagtory,
    newlist
  },
  //为模块创建快捷访问方式
  getters:{
    catagtory: state => state.catagtory.catagtory, // 建立快捷访问
    currentCatagtory: state => state.catagtory.currentCatagtory
  }
})