Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
hezf Avatar asked Nov 01 '25 11:11

hezf


1 Answers

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 }
})
like image 83
nur_riyad Avatar answered Nov 04 '25 05:11

nur_riyad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!