Ask Your Question
2

What is the method for providing an argument to a Pinia store?

asked 2023-05-04 00:47:55 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-04 00:48:02 +0000

nofretete gravatar image

To provide an argument to a Pinia store, you can use the createPinia function and pass the argument as a parameter:

import { createPinia } from 'pinia'

const pinia = createPinia()

const myStore = pinia.defineStore('myStore', {
  state: () => ({
    myArgument: null
  }),
  actions: {
    setMyArgument (value) {
      this.myArgument = value
    }
  }
})

// Provide the argument when creating a new store instance
const storeWithArgument = myStore({ myArgument: 'my value' })

console.log(storeWithArgument.myArgument) // 'my value'

In the example above, the myStore store has a myArgument property in its state, and a setMyArgument action to update its value. To provide an argument to the store instance, call the store function with the argument as the parameter, and access it in the store instance using this.myArgument.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-04 00:47:55 +0000

Seen: 15 times

Last updated: May 04 '23