index.js 497 B

12345678910111213141516171819202122232425
  1. import { defineStore } from "pinia";
  2. export const useCounterStore = defineStore("counter", {
  3. state: () => ({
  4. count: 0
  5. }),
  6. actions: {
  7. increment() {
  8. this.count++
  9. }
  10. }
  11. })
  12. // 使用
  13. /*
  14. <script setup>
  15. import { useCounterStore } from '~/stores/counter'
  16. const counterStore = useCounterStore()
  17. </script>
  18. <template>
  19. <div>
  20. <p>Count: {{ counterStore.count }}</p>
  21. <button @click="counterStore.increment">Increment</button>
  22. </div>
  23. </template>
  24. */