# 热重载 使用 webpack 的 [Hot Module Replacement API](https://webpack.js.org/guides/hot-module-replacement/),Vuex 支持在开发过程中热重载 mutation、module、action 和 getter。你也可以在 Browserify 中使用 [browserify-hmr](https://github.com/AgentME/browserify-hmr/) 插件。 对于 mutation 和模块,你需要使用 `store.hotUpdate()` 方法: ``` js // store.js import { createStore } from 'vuex' import mutations from './mutations' import moduleA from './modules/a' const state = { ... } const store = createStore({ state, mutations, modules: { a: moduleA } }) if (module.hot) { // 使 action 和 mutation 成为可热重载模块 module.hot.accept(['./mutations', './modules/a'], () => { // 获取更新后的模块 // 因为 babel 6 的模块编译格式问题,这里需要加上 `.default` const newMutations = require('./mutations').default const newModuleA = require('./modules/a').default // 加载新模块 store.hotUpdate({ mutations: newMutations, modules: { a: newModuleA } }) }) } ``` 参考热重载示例 [counter-hot](https://github.com/vuejs/vuex/tree/dev/examples/counter-hot)。 ## 动态模块热重载 如果你仅使用模块,你可以使用 `require.context` 来动态地加载或热重载所有的模块。 ```js // store.js import { createStore } from 'vuex' // 加载所有模块。 function loadModules() { const context = require.context("./modules", false, /([a-z_]+)\.js$/i) const modules = context .keys() .map((key) => ({ key, name: key.match(/([a-z_]+)\.js$/i)[1] })) .reduce( (modules, { key, name }) => ({ ...modules, [name]: context(key).default }), {} ) return { context, modules } } const { context, modules } = loadModules() const store = new createStore({ modules }) if (module.hot) { // 在任何模块发生改变时进行热重载。 module.hot.accept(context.id, () => { const { modules } = loadModules() store.hotUpdate({ modules }) }) } ```