Vuex 使用笔记

 

在使用Vuex之前需要使用Vue声明组件 

  1. Vue.use(Vuex)
  2. let store = new Vuex.store({
  3.     state,// Vuex中保存数据 
  4.     mutations, // 对Vuex中的state中的数据进行操作 
  5.     actions, //使用 commit() 对mutations操作
  6.     getters //类似于computed计算属性 
  7. })
  8. new Vue({ store })

需要使用在Vue中绑定 store
Store声明之后回创建一个 $store对象  $store对象中有 Vuex中的state对象 以及 getter对象 可通过$store.state 或者$store.getter 的方法调用
actions对象 需要通过dispatch方法调用 如 
methods:{
        add(){ dispatch("addcount","可以传输的数据") }
}
vuex中的actions 对象
let actions = {   
    addcount({commit,state},val){  
        commit( "addcounts",val )  
    }  
}
Vuex中的mutations 对象
let mutations = {
    addcounts(state,val){ // 
        state.count += val
    }
}
Vuex中的state 对象 
let state = {
    count:0,
}

Vuex中的getters对象 使用方法 
let getters = {
    decrement(state){
        state.count
    }

调用在对应vue组件中
  computed:{
       dec(){
           return this.$store.getters.decrement; //在getters中定义的函数不需要调用 因为为getter方法 在数据读取时会自动调用
       }
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

驴肉火烧荷包蛋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值