Vuex状态管理(State、Mutations、Actions、Getters、mapState、mapGetters、mapActions)

通俗讲:Vuex = 组件传值

       在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值(一般指需要被广泛调用的值,全局变量),值一旦被修改,所有引用该值的地方就会自动更新,Vuex的功能即是如此

一、安装及引入

       1、安装

npm install vuex --save

       2、引入
              我们在项目的src目录下新建一个目录store,在该目录下新建一个index.js文件,我们用来创建vuex实例,然后在该文件中引入vue和vuex,创建Vuex.Store实例保存到变量store中,最后使用export default导出store

// 1、引入vue和vuex
import Vue from 'vue
import Vuex from 'vuex'

// 2、在vue中使用vuex
Vue.use(Vuex);

// 3、创建Vuex的实例
const store = new Vuex.Store({})

// 4、导出store供外部调用
export default store

       然后我们在入口文件main.js文件中引入该文件, import store from ‘./store’,再在vue实例全局引入store对象;

import stor  from './store'

new Vue({
  ....
  store
  ....
})

二、状态说明

       1、State–值的定义
              vuex中的数据源,我们需要保存的数据就保存在这里,可以在页面通过 this.$store.state 来获取我们定义的数据;

import Vue from 'vue
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
    // 定义全局可调用变量count
	state: {
		count: 1
	}
})

export default store

              在任何需要用到count值的地方通过 this.$store.state.count 调用即可

<template>
	<div>
		<span>{{this.$store.state.count}}</span>
 	</div>
</template>

       2、Getters–值的计算与获取
              Getter相当于vue中的computed计算属性,getter 的返回值依据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听state中的值的变化,返回计算后的结果

import Vue from 'vue
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	// 编写getters方法,仅传state一个变量
	getters: {
		getStateCount: function(state) {
			return state.count + 1;
		}
	}
})

export default store

              在任何需要用到count值的地方通过this.$store.state.getters.getters中定义的方法 调用即可

<template>
	<div>
		<!--<span>{{this.$store.state.count}}</span>-->
		<span>{{this.$store.state.getters.getStateCount}}</span>
 	</div>
</template>

       3、Mutations–值的修改(不推荐)
              数据我们在页面是获取到了,但是如果我们需要修改count值怎么办?如果需要修改store中的值唯一的方法就是提交(commit
)mutation来修改,我们现在Hello World.vue文件中添加两个按钮,一个加1,一个减1;这里我们点击按钮调用addFun(执行加的方法)和reductionFun(执行减法的方法),然后在里面直接提交mutations中的方法修改值:

<template>
	<div>
		<span>{{this.$store.state.count}}</span>
		<button @click="addFun">加</button>
		<button @click="reductionFun">减</button>
	</div>
</template>

<script>
	export default {
		methods: {
			addFun () {
				this.$store.commit("add");
			},
			reductionFun () {
			  this.$store.commit("reduction");
			}
		}
	}
</script>

              修改store/index.js文件,添加mutations,在mutations中定义两个函数,用来对count加1和减1,这里定义的两个方法就是上面commit提交的两个方法如下:

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 ;
	  }
	}
})

export default store

3A、Actions–值的修改(标准)
              我们看到,当点击三次后值从2变成了-1;页面上的值是改变了;我们达到了修改store中状态值的目的,但是,官方并不介意我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值,使用 dispatch 接下来我们修改index.js文件,先定义actions提交mutation的函数:
              ①仅传方法名

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,类似于Vue里的methods
	actions: {
	    // 接收一个与store实例具有相同方法名的属性的context对象
		addFun (context) {
			context.commit("add");
		},
		reductionFun (context) {
			context.commit("reduction");
		}
	}
})

export default store
<template>
	<div>
		<span>{{this.$store.state.count}}</span>
		<button @click="addFun">加</button>
		<button @click="reductionFun">减</button>
	</div>
</template>

<script>
	export default {
		methods: {
			addFun () {
				this.$store.dispatch("addFun");
			},
			reductionFun () {
			  this.$store.dispatch("reductionFun");
			}
		}
	}
</script>

              这里我们把commit提交mutations修改为使用dispatch来提交actions;我们点击页面,效果是一样的。
              ②传方法名及参数

<template>
	<div>
		<span>{{this.$store.state.count}}</span>
		<button @click="addFun">加</button>
		<button @click="reductionFun">减</button>
	</div>
</template>

<script>
	export default {
		methods: {
			addFun () {
				this.$store.dispatch("addFun");
			},
			reductionFun () {
				var n = 10;
			  	this.$store.dispatch("reductionFun", n);
			}
		}
	}
</script>
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, n) {
	    	state.count = state.count - n ;
	  }
	},
	// 注册actions,类似于Vue里的methods
	actions: {
	    // 接收一个与store实例具有相同方法名的属性的context对象
		addFun (context) {
			context.commit("add");
		},
		reductionFun (context, n) {
			context.commit("reduction", n);
		}
	}
})

export default store

这个时候我们再去点击“ - ”按钮就会发现不再是减1了,而是减去10了。

              5、便捷写法mapState、mapGetters、mapActions
              如果我们不喜欢这种在页面上使用"this.$ stroe.state.count"和"this.$ store.dispatch(‘funName’)"这种很长的写法,那么我们可以使用mapState、mapGetters、mapActions就不会这么麻烦了;

<template>
	<div>
	    // 3、用计算属性定义的变量显示
		<span>{{cut}}</span>
		<button @click="addFun">加</button>
		<button @click="reductionFun">减</button>
	</div>
</template>

<script>
  	// 1、导入mapState, mapActions, mapGetters
	import { mapState, mapActions, mapGetters } from 'vuex';
	export default {
		// 2、通过计算属性重新赋值给变量
	   computed: {
	   		...mapState({
				cut : state => state.count
			})
	   }
		methods: {
			addFun () {
				this.$store.dispatch("addFun");
			},
			reductionFun () {
			  this.$store.dispatch("reductionFun");
			}
		}
	}
</script>

正常显示,效果是一样的,我们就可以不再使用很长的写法来调用了。

发布了24 篇原创文章 · 获赞 1 · 访问量 529

猜你喜欢

转载自blog.csdn.net/Kasey_L/article/details/104908217