提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.4 KiB

3 weeks ago
  1. <template>
  2. <div class="count">
  3. <h2>当前求和为:{{ countStore.sum }}</h2>
  4. <select v-model.number="n">
  5. <option value="1">1</option>
  6. <option value="2">2</option>
  7. <option value="3">3</option>
  8. </select>
  9. <button @click="add">加</button>
  10. <button @click="minus">减</button>
  11. </div>
  12. </template>
  13. <script setup lang="ts" name="Count">
  14. import { ref,reactive } from "vue";
  15. import {useCountStore} from '@/store/count'
  16. const countStore = useCountStore()
  17. // 以下两种方式都可以拿到state中的数据
  18. // console.log('@@@',countStore.sum)
  19. // console.log('@@@',countStore.$state.sum)
  20. /* let obj = reactive({
  21. a:1,
  22. b:2,
  23. c:ref(3)
  24. })
  25. let x = ref(9)
  26. console.log(obj.a)
  27. console.log(obj.b)
  28. console.log(obj.c) */
  29. // 数据
  30. let n = ref(1) // 用户选择的数字
  31. // 方法
  32. function add(){
  33. //第一种修改方式
  34. // countStore.sum += 1
  35. //第二种修改方式
  36. // countStore.$patch({
  37. // sum:999,
  38. // school:'xxx',
  39. // address:'yyy'
  40. // })
  41. //第三种修改方式
  42. countStore.increment(n.value)
  43. }
  44. function minus(){
  45. countStore.decrement(n.value)
  46. }
  47. </script>
  48. <style scoped>
  49. .count {
  50. background-color: skyblue;
  51. padding: 10px;
  52. border-radius: 10px;
  53. box-shadow: 0 0 10px;
  54. }
  55. select,button {
  56. margin: 0 5px;
  57. height: 25px;
  58. }
  59. </style>