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

存在嵌套且命名空间存在

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.属性
			})
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值