Count.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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">+</button>
  12. <button @click="decrement">-</button>
  13. <button @click="incrementOdd">当前求和为奇数再加</button>
  14. <button @click="incrementWait">等一等再加</button>
  15. </div>
  16. </template>
  17. <script>
  18. import { mapGetters, mapState } from "vuex";
  19. export default {
  20. name: "Count",
  21. data() {
  22. return {
  23. n: 1, //用户选择的数字
  24. };
  25. },
  26. computed: {
  27. // 靠程序员自己亲自去写计算属性
  28. // sum(){
  29. // return this.$store.state.sum
  30. // },
  31. // school(){
  32. // return this.$store.state.school
  33. // },
  34. // subject(){
  35. // return this.$store.state.subject
  36. // },
  37. // 借助mapState生成计算属性,从state中读取数据(对象写法)
  38. // ...mapState({ he: "sum", xuexiao: "school", xueke: "subject" }),
  39. // 借助mapState生成计算属性,从state中读取数据(数组写法)
  40. ...mapState(["sum", "school", "subject"]),
  41. /*********************************** */
  42. // bigSum(){
  43. // return this.$store.getters.bigSum
  44. // }
  45. // 借助mapGetters生成计算属性,从state中读取数据(对象写法)
  46. // ...mapGetters({ bigSum: "bigSum" }),
  47. // 借助mapGetters生成计算属性,从state中读取数据(数组写法)
  48. ...mapGetters(["bigSum"]),
  49. },
  50. mounted() {
  51. // const x = mapState({he:'sum',xuexiao:'school',xueke:'subject'})
  52. // console.log(x)
  53. },
  54. methods: {
  55. increment() {
  56. // this.$store.dispatch('jia',this.n)
  57. this.$store.commit("JIA", this.n);
  58. },
  59. decrement() {
  60. // this.$store.dispatch('jian',this.n)
  61. this.$store.commit("JIAN", this.n);
  62. },
  63. incrementOdd() {
  64. this.$store.dispatch("jiaOdd", this.n);
  65. },
  66. incrementWait() {
  67. this.$store.dispatch("jiaWait", this.n);
  68. },
  69. },
  70. };
  71. </script>
  72. <style>
  73. button {
  74. margin-left: 5px;
  75. }
  76. </style>