首先要知道,咱们说的Vuex是什么?javascript
官方给出的解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的全部组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。html
好吧,看到这,相信不少人都会说:这是啥啊这。vue
开始我也是懵逼状态,后来我想到了一个比方:java
vue init webpack-simple vuex-demo
cd vuex-demo
npm installwebpack
npm install vuex -Ses6
store.js 中写入web
import Vue from 'vue' //引入 vuex 并 use import Vuex from 'vuex' Vue.use(Vuex)
main.js 文件vuex
import Vue from 'vue' import App from './App.vue' import store from './assets/store' //导入 store 对象 new Vue({ //配置 store 选项,指定为 store 对象,会自动将 store 对象注入到全部子组件中,在子组件中经过 this.$store 访问该 store 对象 store, el: '#app', render: h => h(App) })
在应用 vuex 以前,咱们仍是须要看懂这个流程图,其实很简单。
npm
state 定义属性(状态 、数据)
getters 用来获取属性
actions 定义方法(动做)
commit 提交变化,修改数据的惟一方式就是显示的提交 mutations
mutations 定义变化,处理状态(数据)的改变
mapGetters 用来获取属性(数据)
mapActions 用来获取方法(动做)数组
store.js 中写入
// 定义属性(数据) var state = { count:6 } // 建立 store 对象 const store = new Vuex.Store({ state }) // 导出 store 对象 export default store;
this.$store
访问该 store 对象 ,获取该 count
。其中须要注意的是
this.$store
中的 store 与 main.js 中配置的 store 相对应,必定要注意大小写
<template> <div id="app"> //把 count 方法直接写入,可本身执行 <h1>{{count}}</h1> </div> </template> <script> export default { name: 'app', computed:{ count(){ //返回获取到的数据 return this.$store.state.count } } } </script>
执行
npm run dev
就能在页面中看到传过来的数据了
mapGetters 用来获取属性(数据)
① 在 app.vue 中引入 mapGetters
import {mapGetters} from 'vuex'
② 在 app.vue 文件的计算属性中调用 mapGetters 辅助方法,并传入一个数组,在数组中指定要获取的属性 count
<script> import {mapGetters,mapActions} from 'vuex' export default { name: 'app', computed:mapGetters([ //此处的 count 与如下 store.js 文件中 getters 内的 count 相对应 'count' ]) } </script>
③ 在 store.js 中定义 getters 方法并导出
getters 用来获取属性
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 定义属性(数据) var state = { count:6 } // 定义 getters var getters={ //须要传个形参,用来获取 state 属性 count(state){ return state.count } } // 建立 store 对象 const store = new Vuex.Store({ state, getters }) // 导出 store 对象 export default store;
这样页面上就会显示传过来的数据了!接下来咱们来经过动做改变获取到的数据
④在 store.js 中定义 actions 和 mutations 方法并导出
actions 定义方法(动做)
commit 提交变化,修改数据的惟一方式就是显示的提交 mutations
mutations 定义变化,处理状态(数据)的改变
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 定义属性(数据) var state = { count:6 } // 定义 getters var getters={ count(state){ return state.count } } // 定义 actions ,要执行的动做,如流程的判断、异步请求 const actions ={ // ({commit,state}) 这种写法是 es6 中的对象解构 increment({commit,state}){ //提交一个名为 increment 的变化,名字可自定义,能够认为是类型名,与下方 mutations 中的 increment 对应 //commit 提交变化,修改数据的惟一方式就是显式的提交 mutations commit('increment') } } // 定义 mutations ,处理状态(数据) 的改变 const mutations ={ //与上方 commit 中的 ‘increment’ 相对应 increment(state){ state.count ++; } } // 建立 store 对象 const store = new Vuex.Store({ state, getters, actions, mutations }) // 导出 store 对象 export default store;
⑤ 在 app.vue 中引入 mapActions ,并调用
mapActions 用来获取方法(动做)
import {mapGetters,mapActions} from 'vuex'
调用 mapActions 辅助方法,并传入一个数组,在数组中指定要获取的方法 increment
<template> <div id="app"> //这个 increment 方法与下面 methods 中的 increment 相对应 <button @click="increment">增长</button> <button>减小</button> <h1>{{count}}</h1> </div> </template> <script> import {mapGetters,mapActions} from 'vuex' export default { name: 'app', computed:mapGetters([ 'count' ]), methods:mapActions([ //该 increment 来自 store.js 中导出的 actions 和 mutations 中的 increment 'increment', ]) } </script>
这样就能经过 button
来改变获取到的 count
了。
如今你能够经过
storel.state
来获取状态对象,以及经过store.commit
方法触发状态变动注意:咱们经过提交
mutation
的方式,而非直接改变store.state.count
,是由于咱们想更明确地追踪到状态的变化。
这是经过点击事件让 count++
,不妨本身试着写一下 count--
吧!