Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式
1,安装
npm install vuex
2,创建文件
在src>store>index.js
import Vue from 'vue'
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
state: { //状态
},
getters: { //就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。
},
mutations: { //更改 Vuex 的 store 中的状态的唯一方法是提交 mutation
},
actions: { //actions不是必须的,但是在运用到异步时就要用到actions例如setTimeOut
}
})
3,使用
在mian.js
import store from './store' // 引入store
...
new Vue({
el: '#app',
router,
store,//使用vuex
components: { App },
template: '<App/>'
})