1、项目new-project的src下建立一个store.js.
const指示常量。
export default new Vuex.Store({
state,
mutations,
getters,
actions
})
这个export default new Vuex.Store 注意Store的s必须大写,不然会报错没有这个构造方法: Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_1_vuex__.a.sto。
另外,这个Store里的各个组件的名字是固定的,就是这些,注意复数形式。
![](https://img.haomeiwen.com/i15558695/cbb62cd493aa2c4f.png)
2、main.js中引入store.js
![](https://img.haomeiwen.com/i15558695/bbe0281797e36f0e.png)
3 、页面上添加部分内容,测试是否用到了store.js
![](https://img.haomeiwen.com/i15558695/f819028f6cea822d.png)
4、vue实例中使用vuex
![](https://img.haomeiwen.com/i15558695/f2a29382d2e89604.png)
网友评论