Vue的学习(17)

优化昨日Demo

<template>
  <div>
    <p>click {{count}} times, count is {{evenOrOdd}}</p>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementIfOdd">increment if odd</button>
    <button @click="incrementAsync">increment async</button>
  </div>
</template>

<script>
  import {mapState, mapGetters, mapActions} from 'vuex'
  export default {

    computed:{
      ...mapState(['count']),
      //mapState()返回值:{count(){return this.$store.count}}
      ...mapGetters(['evenOrOdd']),
     //  count(){
     //    return this.$store.count
     //  },
     // evenOrOdd(){
     //   return this.$store.getters.evenOrOdd
     // }
    },
    methods:{
      ...mapActions(['increment', 'decrement', 'incrementIfOdd', 'incrementAsync'])
    }

    
    // // 所有用 vuex 管理的组件中都多了一个属性$store, 它就是一个 store 对象
    // methods:{
    //   increment(){
    //     //通知vuex去增加
    //     this.$store.dispatch('increment')//触发store中action调用
    //   },
    //   decrement(){
    //     this.$store.dispatch('decrement')
    //   },
    //   incrementIfOdd(){
    //     this.$store.dispatch('incrementIfOdd')
    //   },
    //   incrementAsync(){
    //     this.$store.dispatch('incrementAsync')
    //   },
    // },
  }
</script>

<style>

</style>

…mapActions([‘increment’, ‘decrement’, ‘incrementIfOdd’, ‘incrementAsync’])
这些值就是store中的对应值,尽量一一对应名字,如果store中名字是evenOrOdd2,想用evenOrOdd 的话,记忆需要这样写:…mapGetters({evenOrOdd :'evenOrOdd’2}),

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值