Vuex命名空间的所有写法

第一种写法:使用带命名空间名称(奇葩)

命名空间:匹配store路径的时候需要在上对应模块的名称,state比较特殊,需要定义成函数的形式,getters通常会写在全局

​​​​​​​第一种写法:使用带命名空间名称(奇葩)

导入:import { mapAction } from 'vuex

  解构:  ...mapAction(['login/setAsync'])
  使用:this['login/setAsyncToken']('1')

第二种写法: 解析命名空间(繁琐)

   导入:import {  createNamespacedHelpers } from "vuex";
              const { mapActions } = createNamespacedHelpers('login')

    解构:  ...mapAction(['setAsyncToken'])
    使用:this['setAsyncToken']('1')

第三种写法:给函数重命名(高级)


    导入:import { mapAction } from 'vuex'
    解构:...mapActions({setAsyncToken:"login/setAsyncToken"}),
    使用:this.setAsyncToken("1");

注意:前两种做一个了解,认识即可,第三种是万能公式,适用于各种情况

使用示例:


    // state ->
    // 1.this.$store.state.模块名.title
    // 2...mapState({name:state=>state.模块名.title})

    // getters ->
    // 1.this.$store.getters.模块名.title
    // 2...getters({name:state=>state.模块名.title})

    // mutations ->
    // 1.this.$store.commit('article/setTitle',参数)
    // 2....mapMutations({setTitle:'article/setTitle'})  自定义的方法名: '模块名/方法名'

    // action ->
    // 1.this.$store.dispatch('article/setTitle',参数)
    // 2....mapAction({setAsyncTitle:'article/setAsyncTitle'})  自定义的方法名: '模块名/方法名'

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值