搜索文档
Action 相当于组件中的 method。它们可以通过 defineStore() 中的 actions 属性来定义,并且它们也是定义业务逻辑的完美选择。
defineStore()
actions
1import {defineStore} from "pinia"; 2 3export const useCounterStore = defineStore('counter', { 4 5 state: () => ({count: 0}), 6 7 actions: { 8 increment() { 9 this.count++ 10 }, 11 }, 12})
类似 getter,action 也可通过 this 访问整个 store 实例,并支持完整的类型标注(以及自动补全)。不同的是,action 可以是异步的,你可以在它们里面 await 调用任何 API,以及其他 action!
this
action
await
1const store = useCounterStore() 2 3store.increment()