Pinia 第二章(getter, actions)

42 阅读1分钟
import { defineStore } from 'pinia'

const p = function () {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('林宥嘉')
    }, 1000)
  })
}
export const useTestStore = defineStore('test', {
  state: () => ({
    count: 1,
    name: 'yoga',
  }),
  getters: {
    doubleCount: state => state.count * 2,
  },
  actions: {
    add() {
      this.count++
    },
    async get() {
      // 异步操作
      const name = await p()
      this.name = name
    },
  },
})