1、npm install vuex --save;// 安装vuex
2、使用
- 1、创建一个store文件夹,创建index.js文件 ;
- 2、编辑index.js文件
- 3、 在main.js中导入 index.js
-
- 4、 在页面中读取状态:{{$store.state.count}}
- 5、操作store 的数据:
- 5.1、在index.js中 使用 mutations 创建方法;
-
- 5.2 在页面创建方法引用 mutations 中的方法的名称;
- 5.3 使用该方法。
- 6、使用actions 来操作store的数据:
- 6.1、
- 6.2、
- 7 、getters
- 7.1、
- 7.2、