1.安装vuex
npm install vuex --save
2.在src里面创建一个store的文件夹,在store文件夹里面建一个index.js
import Vue from 'vue'
import Vuex frim 'vuex'
//1.安装插件
Vue.use(Vuex)
//2.创建对象
const store = new Vuex.store({})
//3.导出store独享
export default store
3.在main.js里面导入store
//导入store
import store from 'store'
//全局挂载store
Vue.prototype.$store = store
new Vue({
el:'#app',
store,
render:h =>h(App)
})
4.vuex是用来在组件之间共享数据的,它的结构是这样的
state里面是存储的数据,vue components用于展示state里面的数据,actions用于异步改变state里面的数据,而mutations用于同步改变state里面的数据,devtools用于监听mutations的改变,backend API表示后端,
5.
6. 每个vuex里面只有一个state对象,state是用于储存信息的
7. Getters的功能类似于计算属性,
8. mutation的功能就是通过commit同步改变state中储存的数据
9. actions的功能就是通过dispatch异步改变state中储存的数据
10.module的功能就是建立多个储存模块,可以保存多个state