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
.
Asked: 2023-05-04 00:47:55 +0000
Seen: 16 times
Last updated: May 04 '23