pinia 在操作中使用 state 而不是 this

hez*_*ezf 4 vue.js vuejs3 pinia

在Vue3编译API\xef\xbc\x8c中我通常不使用this

\n

但 pinia 的例子是:

\n
increment() {\n  this.counter++\n},\n
Run Code Online (Sandbox Code Playgroud)\n

不想this在行动中使用。有什么建议吗?

\n

nur*_*yad 7

您可以使用函数(类似于组件 setup())来定义 Store。然后你可以声明 actions 和 getter 而无需调用它。官方文档链接

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  function increment() {
    count.value++
  }

  return { count, increment }
})
Run Code Online (Sandbox Code Playgroud)