hezf
hezf

Reputation: 177

pinia use state instead of this in actions

In Vue3 compisition API,I don't usually use this

But pinia example is:

increment() {
  this.counter++
},

Dont want use this in action.Has any suggest?

Upvotes: 4

Views: 5129

Answers (1)

nur_riyad
nur_riyad

Reputation: 1380

You can use a function (similar to a component setup()) to define a Store. Then you can declear actions and getters without calling this. Official Doc Link

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

  return { count, increment }
})

Upvotes: 7

Related Questions