vuex映射属性详解mapState、mapGetters、mapMutations、mapActions、modules的使用(二)

本文详细介绍了Vue.js中模块化配置的三种情况:存在嵌套且命名空间存在、命名空间不存在以及命名空间全局和局部混用的情况。针对每种情况,分别阐述了state、getters、mutations和actions的映射关系使用方法,包括如何直接访问模块内的属性和方法,以及如何进行重命名操作。对于理解和实践Vue.js的模块化管理具有指导意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

存在嵌套且命名空间存在

namespaced:true存在
modules:{
		模块名字:{
        namespaced:true,
        state:()=>({}),
        getters:{},
        mutions:{},
        actions:{}
      }
}
映射关系的使用
	state如何使用   使用的时候直接使用属性名
			...mapState("模块名字",["属性名"])
			...mapState({
				属性名:(state)=>state.模块名.属性名
			})
	getters		直接使用方法名
			...mapGetters(["模块名字/方法"])
			...mapGetters({
				方法:(state)=>state.模块名.方法
			})
	mutions如何使用 直接使用方法名
			...mapMutations(["模块名字/方法"])
			...mapMutations({
				方法:(state)=>state.模块名.方法
			})
	actions	直接使用方法名
			...mapActions(["模块名字/方法名"])
			...mapActions({
				方法:(state)=>state.模块名.方法
			})

存在嵌套且命名空间不存在

namespaced:false 或者就没写这个属性
modules:{
		模块名字:{
        state:()=>({}),
        getters:{},
        mutions:{},
        actions:{}
      }
}
映射关系的使用
	state如何使用   使用的时候直接使用属性名
			...mapState({
				属性名:(state)=>state.模块名.属性名
			})
	getters		直接使用方法名
			...mapGetters(["方法"])
			...mapGetters({
				方法:"方法名"
			})
	mutions如何使用 直接使用方法名
			...mapMutations(["方法"])
			...mapMutations({
				方法:"方法名"
			})
	actions	直接使用方法名
			...mapActions(["方法名"])
			...mapActions({
				方法:"方法名"
			})

存在嵌套存在命名空间全局和局部混用和方法和属性重命名

namespaced:true存在
modules:{
		模块名字:{
        namespaced:true,
        state:()=>({}),
        getters:{},
        mutions:{},
        actions:{}
      }
}
映射关系的使用
	🌰state如何使用   使用的时候直接使用属性名
			...mapState("模块名字",["属性名"])
			
			...mapState("模块名字",{
					名字:属性名字
			})
			
			...mapState({
				属性名:(state)=>state.模块名.属性名, //模块属性
				属性名:(state)=>state.属性   //全局属性
			})
	🌰getters		直接使用方法名
			...mapGetters(["模块名字/方法"])
			
			...mapGetters("模块名",{
				方法:"方法名"
			})
			
			...mapGetters({
				方法:(state)=>state.模块名.方法,  //模块方法
				方法:(state)=>state.方法				//全局方法
			})
	🌰mutions如何使用 直接使用方法名
			...mapMutations(["模块名字/方法"])
			
			...mapMutations("模块名",{
				方法:"方法名"
			})
			
			...mapMutations({
				方法:(state)=>state.模块名.方法, //模块方法
				方法:(state)=>state.方法		//全局方法
			})
	🌰actions	直接使用方法名
			...mapActions(["模块名字/方法名"])
			
			...mapActions("模块名",{
				方法:"方法名"
			})
			
			...mapActions({
				方法:(state)=>state.模块名.方法, //模块方法
				方法:(state)=>state.方法		//全局方法
			})
			
			
属性或者方法的重命名  mapGetters mapMutations mapActions 同理 举个🌰mapState的
		...mapState({
				属性名(可赋值为和state属性名不同的字符串):(state)=>state.模块名.属性名, //模块属性
				属性名(可赋值为和state属性名不同的字符串):(state)=>state.属性
			})
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值