Count.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div>
  3. <h1>当前求和为:{{ sum }}</h1>
  4. <h1>当前求和放大10倍为:{{ bigSum }}</h1>
  5. <h1>我在{{ school }},学习{{ subject }}</h1>
  6. <select v-model.number="n">
  7. <option value="1">1</option>
  8. <option value="2">2</option>
  9. <option value="3">3</option>
  10. </select>
  11. <button @click="increment(n)">+</button>
  12. <button @click="decrement(n)">-</button>
  13. <button @click="incrementOdd(n)">当前求和为奇数再加</button>
  14. <button @click="incrementWait(n)">等一等再加</button>
  15. </div>
  16. </template>
  17. <script>
  18. import { mapGetters, mapState, mapMutaions } from "vuex";
  19. export default {
  20. name: "Count",
  21. data() {
  22. return {
  23. n: 1, //用户选择的数字
  24. };
  25. },
  26. computed: {
  27. // 借助mapState生成计算属性,从state中读取数据(对象写法)
  28. // ...mapState({ he: "sum", xuexiao: "school", xueke: "subject" }),
  29. // 借助mapState生成计算属性,从state中读取数据(数组写法)
  30. ...mapState(["sum", "school", "subject"]),
  31. /*********************************** */
  32. // 借助mapGetters生成计算属性,从state中读取数据(对象写法)
  33. // ...mapGetters({ bigSum: "bigSum" }),
  34. // 借助mapGetters生成计算属性,从state中读取数据(数组写法)
  35. ...mapGetters(["bigSum"]),
  36. },
  37. mounted() {
  38. // const x = mapState({he:'sum',xuexiao:'school',xueke:'subject'})
  39. // console.log(x)
  40. },
  41. methods: {
  42. //程序员亲自写方法
  43. /* increment(){
  44. this.$store.commit('JIA',this.n)
  45. },
  46. decrement(){
  47. this.$store.commit('JIAN',this.n)
  48. }, */
  49. //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(对象写法)
  50. ...mapMutations({ increment: "JIA", decrement: "JIAN" }),
  51. //借助mapMutations生成对应的方法,方法中会调用commit去联系mutations(数组写法)
  52. // ...mapMutations(['JIA','JIAN']),
  53. /* ************************************************* */
  54. //程序员亲自写方法
  55. /* incrementOdd(){
  56. this.$store.dispatch('jiaOdd',this.n)
  57. },
  58. incrementWait(){
  59. this.$store.dispatch('jiaWait',this.n)
  60. }, */
  61. //借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(对象写法)
  62. ...mapActions({ incrementOdd: "jiaOdd", incrementWait: "jiaWait" }),
  63. //借助mapActions生成对应的方法,方法中会调用dispatch去联系actions(数组写法)
  64. // ...mapActions(['jiaOdd','jiaWait'])
  65. },
  66. };
  67. </script>
  68. <style>
  69. button {
  70. margin-left: 5px;
  71. }
  72. </style>