Vuex是什么呢?它是Vue的状态管理模式,在使用vue的时候,须要在vue中各个组件之间传递值是很痛苦的,在vue中咱们可使用vuex来保存咱们须要管理的状态值,值一旦被改变,全部引用该值的地方就会自动更新。是否是很方便,很好用呢?vue
vuex是专门为vue.js设计的状态管理模式,集中式存储和管理应用程序中全部组件的状态,vuex也集成了vue的官方调式工具,一个vuex应用的核心是store,一个容器,store包含了应用中大部分状态。webpack
那么咱们在何时应用vuex呢?vuex也不是随便乱用的,小型简单的应用就不那么合适了,由于用了Vuex是繁琐多余的,更适合使用简单的store模式;对于vuex更加适用于中大型单页应用:多个视图使用于同一状态,不一样视图须要变动同一状态。web
实现组件间数据共享集中式存储和管理应用程序中全部组件的状态vuex
一个 Vuex 应用的核心是 store(仓库,一个容器),store包含着你的应用中大部分的状态 (state)。npm
传参的方法对于多层嵌套的组件来讲,是很是繁琐的,而且对于兄弟组件间的状态传递无能为力;采用父子组件直接引用或者经过事件来变动和同步状态的多份拷贝,一般会致使没法维护的代码。缓存
什么状况下我应该使用 Vuex
适用于:中大型单页应用,无论在哪一个组件,都能获取状态/触发行为架构
解决问题以下:
① 多个视图使用于同一状态:app
传参的方法对于多层嵌套的组件将会很是繁琐,而且对于兄弟组件间的状态传递无能为力异步
② 不一样视图须要变动同一状态:工具
采用父子组件直接引用或者经过事件来变动和同步状态的多份拷贝,一般会致使没法维护的代码
安装命令:
在项目文件夹下vue install vuex
建立一个vue项目,输入vue int webpack web,安装vuex,命令:npm install vuex --save。
store,index.js
import Vue from 'vue' // 引入vue import Vuex from 'vuex' // 引入vuex // 使用vuex Vue.use(Vuex); // 建立Vuex实例 const store = new Vuex.store({ }) export default store // 导出store
main.js
import Vue from 'Vue' import App from './App' import router from './router' import store from '.store' Vue.config.productiontip = false new Vue({ el: '#app', store, router, components: {App}, ... })
State,能够在页面经过this.$store.state来获取咱们定义的数据:
import Vue from 'vue' // 引入vue import Vuex from 'vuex' // 引入vuex // 使用vuex Vue.use(Vuex); // 建立Vuex实例: const store = new Vuex.Store({ state: { count: 1 } }) export default store // 导出store {{this.$store.state.count}}
Getters至关于vue中的computed计算属性,getter的返回值根据它的依赖被缓存起来,且只有当它的依赖值发生改变时才会从新计算。
Getters能够用于监听,state中的值的变化,返回计算后的结果。
{{this.$store.getSateCount}} import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); const store = new Vuex.Store({ state: { count: 1; }, getters: { getStateCount: function(state){ return state.count+1; } }
Mutations
{{this.$store.state.count}} <button @click="addFun">+</button> <button @click="reductionFun">-</button> methods: { addFun() { this.$store.commit("add"); }, reductionFun() { this.$store.commit("reduction"); } }
index.js
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); // 建立Vuex实例 const store = new Vuex.store({ state: { count: 1 }, getters: { getStateCount: function(state){ return state count+1; } }, mutations: { add(state) { state.count = state.count+1; }, reduction(state){ state.count = state.count-1; } } }) export default store // 导出store
Actions:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); const store = new Vuex.Store({ state: { count: 1; }, getters: { getStateCount: function(state){ return state.count+1; } } mutations: { add(state) { state.count = state.count+1; }, reduction(state) { state.count = state.count-1; } }, actions: { addFun(context) { context.commit("add"); }, reductionFun(context) { context.commit("reduction"); } } // vue methods: { addFun() { this.$store.dispatch("addFun"); // this.$store.commit("add"); }, reductionFun() { this.$store.dispatch("reductionFun"); } }
传值:
methods: { addFun() { this.$store.dispatch("addFun"); // this.$store.commit("add"); }, reductionFun() { var n = 10; this.$store.dispatch("reductionFun", n); } } mutations: { add(state) { state.count = state.count+1; }, reduction(state,n) { state.count = state.count-n; } }, actions: { addFun(context) { context.commit("add"); }, reductionFun(context,n) { context.commit("reduction",n); } }