一、Vuex了解,附带调用方法

 

 

-------------------------------------------------------------------------------------------------------------------------------- 

state:{ 数据 }

    1.this.$store.state.数据名称
    2.computed:{
        ...mapState(['数据'])
      }

mutations:{ 方法 }

    1.this.$store.commit('方法',参数)
    2.methods: {
        ...mapMutations(["addCounter"])
      }

actions:{ 异步函数 }

    1.this.$store.dispatch(方法,参数)
    2.methods: {
        ...mapActions(["addCounter"])
      }
    

getters:{ 计算属性 }

    1.this.$store.getters.()
    2.computed:{
        ...mapGetters(['数据'])
      }
​
    // vuex
    getters:{
        changeData (context) {
            context.commit('increment')
        }
    }

module:{ 模块化 }

特殊的this调用方法

this.$store.dispatch('user/方法')

this['user/方法'].()

建议第三种

createNamespacedHelpers方法

    {
        // 模块 user
        user:{
            namespaced : true, // 开启命名空间
            state:{},
            mutations:{}
        }
    }
    
    // 默认的模块化是全局,开启模块化之后调用
​
    1.this.$store.dispatch('user/方法')
    2.methods: {
        ...mapActions(["user/addCounter"])
        
        // 调用Vuex模块化方法
        change(){
            this['user/方法'].()
        }
        
        // 特别注意这里的 this 调用方法
        `this['user/方法'].()`
        
      }
    3.
    // 1.导入createNamespacedHelpers方法,createNamespacedHelpers方法可以指定模块名称
    import { createNamespacedHelpers } from 'vuex'
    const mapGetter = createNamespacedHelpers('user')
    
    computed:{
        ...mapGetter(['name'])
    }
    

模块化的命名空间

模块:{

state:{ }

}

state调用

a.this.$store.模块.数据

b.

      // 可以实现,模块的state全局调用  和全局state调用一样
      主 getters:{
          数据名:state => { state.模块名.数据 }
      }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值