counter.js 560 B

1234567891011121314
  1. import { defineStore } from 'pinia'
  2. // 你可以任意命名 `defineStore()` 的返回值,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。
  3. // (比如 `useUserStore`,`useCartStore`,`useProductStore`)
  4. // 第一个参数是你的应用中 Store 的唯一 ID。
  5. export const useCounterStore = defineStore('counter', () => {
  6. const count = ref(0) // state
  7. const doubleCount = computed(() => count.value * 2) // getters
  8. function increment() { // actions
  9. count.value++
  10. }
  11. return { count, doubleCount, increment }
  12. })