Vuex的基本使用

一 Vuex的基本使用

1.安装 vuex依赖包

npm install vuex --save

2.导入vuex 包

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(vuex);

3.创建store对象

const store = new Vuex.Store({
	//state存放的就是全局共享的数据
	state:{count:0}
})

4.将store对象挂载到vue实例中

new Vue({
	el:'#app',
	render:h=>h(app),
	router,
	//将创建的共享数据对象挂载到vue实例中,
	//所有的组件就可以在store 中获取全局的数据
	store
	
})

二 Vuex的核心概念

Vuex的主要核心概念:

2.1State
State提供唯一的公共数据源,所有共享的数据都要统一放到Store 的State中进行存储。

//创建Store数据源,提供唯一的公共数据
const store = new Vuex.Store(
{
	state:{count:0}
})

访问state数据的第一种方法:

this.$store.state.count

如下:

<template>
<div>
<h3>当前最新的count值为:{{this.$store.state.count}}</h3>
</div>
</template>
<script>
export default {
	data(){
		return{}
	}
}
</script>

在这里插入图片描述
组件访问State中数据的第二种的方式:

//从vuex中按需导入mapState函数
import {mapState} from 'vuex'

通过刚才导入的mapState函数,将当前组件需要的全局数据。映射为当前组件的computed计算属性;

//将全局数据,映射为当前的计算属性
computed:{
	...mapState(['count'])//需要哪个全局的数据(count),就放在里面声明
	}
<template>
<div>
<h3>当前最新的count值为:{{count}}</h3>
</div>
</template>
<script>
export default {
	data(){
		return{}
	},
	computed:{
		...mapState(['count'])
}
</script>

2.2Mutation
Mutation用于变更Store中的数据。
a.只能通过mutation变更store中的数据,不可以直接操作Store中的数据
b.通过这种方式索然繁琐,但是可以集中监控所有数据的变化。


const store = new Vuex.Store({
	state:{count:0},
	//定义mutation
	mutation:{
		//接受state对象,获取state对象里面的数据
		add(state){
			//变更状态
			state.count++;
		}
	}

那么如何调用mutation中的add方法中呢,可以在methods中写个点击方法通过commit调用mutation的add方法。

<template>
<div>
<h3>当前最新的count值为:{{count}}</h3>
<button @click='handlel'>加1</button>
</div>
</template>
<script>
export default {
	data(){
		return{}
	},
	computed:{
		...mapState(['count'])
		},
	methods:{
		handlel(){
			//触发mutation中的add方法
			this.$store.commit('add');
	}
}
}
</script>

在这里插入图片描述
c.可以在触发mutations时传递参数

//定义mutation
const store = new Vuex.Store({
	state:{count:0},
	mutations:{
		addn(state,step){
			//变更状态
			state.count+=step
			}
		}
	})
//触发mutation
methods:{
	handle2(){
		//在调用commit函数。commit函数的作用是调用某个mutations的方法
		//触发mutations时传递参数
		this.$store.commit('addn',3)
		}
	}

this.$store.commit()是触发mutations的第一种方式,触发mutations的第二种方式。

//1.在当前组件从Vuex中按需导入mapMutations函数
import {mapMutations} from 'vuex'

通过刚才导入的mapMutations函数,映射为当前组件的methods函数

methods:{
	...mapMutations(['add','addn'']),
	handlel(){
		this.add();//触发mutations,调用mutations的add方法
		},
	handle2(){
		this.addn(3);//触发mutations,调用mutations的addn方法,3是传递给mutations中addn的参数。
	}
}

2.3Action
Action用于处理异步任务
如果通过异步操作变更数据,必须通过Action,而不能使用Mutation,但是在Action中还是要通过触发Mutation的方式间接变更数据。

//定义Action
const store = new Vuex.Store({
	state:{count:0},
	mutations:{
		add(state){
			state.count+=1}
			},
	actions:{
		//context是store的实例,固定的写法
		addAsync(context){
			setTimeout(()=>{
				//在actions中,不能直接修改state中的数据
				//只能通过context.commit()触发某个mutations中的方法才行。
				context.commit('add')
				},1000)//1秒之后触发mutations中的add方法
			}
		}
	})
//触发action
methods:{
	handle(){
		//在当前组件触发actions
		//触发actions的第一种方式
		this.$store.dispatch('addAsync')
		}
	}

解决actions异步任务时携带参数:

//定义Action
const store = new Vuex.Store({
	state:{count:0},
	mutations:{
		//接收actions传递过来的参数
		add(state,step){
			state.count+=step
			}
		},
	actions:{
		//context是store的实例,固定的写法
		addAsync(context,step){//step是methods中的方法传递过来的参数
			setTimeout(()=>{
				//在actions中,不能直接修改state中的数据
				//只能通过context.commit()触发某个mutations中的方法才行。
				context.commit('add',step)//将接受到的参数step传给mutations
				},1000)//1秒之后触发mutations中的add方法
			}
		}
	})
//触发action
methods:{
	handle(){
		//在当前组件触发actions,调用dispatch函数
		//触发actions时携带参数5
		this.$store.dispatch('addAsync',5)
		}
	}

触发actions的第二种方法:

//在当前组件从vuex中按需导入mapActions函数
import {mapActions} from 'vuex'
methods:{
	...mapActions(['add','addn'']),
	handlel(){
		this.add();//触发actions,调用actions的add方法
		},
	handle2(){
		this.addn(3);//触发actions,调用actions的addn方法,3是传递给actions中addn的参数。
	}
}

2.4getter
getter不能修改state里面的数据,只是对state里面的数据进行包装

//定义getter
const store = new Vuex.Store({
	state:{
		count:0
		},
	getters:{
		//接收state参数
		showNum: state=>{
			return '当前的数量时【'+state.count+']'//获取state里面的数据
			}
		}
	})

使用getter的第一种方式:

this.$store.getters.名称

使用getters的第二种方式:

import {mapGetters} from 'vuex'
computed:{
	...mapGetters(['showNum'])
	}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生哥编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值