counter.js 744 Bytes
// Pinia 入门文档:https://pinia.vuejs.org/introduction.html
import { defineStore } from 'pinia'

/**
 * @description 计数器示例 Store(模板保留)
 */
export const useCounterStore = defineStore('counter', {
  state: () => {
    return { count: 0 }
  },
  // 也可以写成:state: () => ({ count: 0 })
  actions: {
    /**
     * @description 计数 +1
     * @returns {void} 无返回值
     */
    increment() {
      this.count++
    }
  }
})

// 也可以用函数式(类似组件 setup)定义 Store,适合更复杂场景:
// export const useCounterStore = defineStore('counter', () => {
//   const count = ref(0)
//
//   function increment() {
//     count.value++
//   }
//
//   return {count, increment}
// })