注意事项

严格模式下的禁忌

开启严格模式,仅需在创建 store 的时候传入 strict: true:

const store = new Vuex.Store({
  // ...
  strict: true,
});
1
2
3
4
  1. 在严格模式下,state 必须是 mutation 修改,不然就会报错,非严格模式则不会。
  2. 非严格模式下,我们甚至可以把 state 绑定到表单元素的 v-model 上,严格模式不可以
  3. 非严格模式下,我们的 state 在 actions 里也可以修改。

开发环境与发布环境

不要在发布环境下启用严格模式!严格模式会深度监测状态树来检测不合规的状态变更——请确保在发布环境下关闭严格模式,以避免性能损失。

const store = new Vuex.Store({
  // ...
  strict: process.env.NODE_ENV !== "production",
});
1
2
3
4

Last Updated:
Contributors: websong