vuex模块化,vuex数据持久化的问题

目录结构
想要实现vuex 模块化,直接在index.js 这样改,就可以动态导入写在camera,user文件里面的子模块

index.js 文件


import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"   //插件数据持久化
Vue.use(Vuex)

const store = new Vuex.Store({
  plugins: [createPersistedState()]   //插件数据持久化
})

function registerStoreModule(module) {
  const path = module.path
  if (!path || store.hasModule(path)) return
  const _module = Object.assign({}, module)
  delete _module.path
  // 加上preserveState: true 可以配置插件,数据持久化,如果没有使用vuex-persistedstate插件,就不要加上
  store.registerModule(path, _module, { preserveState: true })
}

const storeContexts = require.context('./modules', true, /index\.js$/)
storeContexts.keys().forEach(name => {
  registerStoreModule(storeContexts(name).default)
})
export default store

user子模块里的index.js

const user = {
  path: 'user',
  namespaced: true,
  state: {
    // 用户信息
    userInfo: null,
  },
  mutations: {
  },
  actions: {

    },

  },

}

export default user

在入口文件,直接引入 store

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值