-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup store - A more comprehensive example #978
Comments
Thanks for the code sample! I added it to the docs roadmap |
Is there any way to avoid re-enumerating all of the states, getters, and actions in the return, (or at least automate the entries)?
|
@jenstornell Is this transparent to store clients? I.e. does the code below still work just as if the store was defined statically with option syntax? const store = useStoreCounter()
const { count, isEven, messageIfEven } = storeToRefs(store)
const { increment } = store |
passing an option object gets complex and brittle with all the `this`. Moreover it's not entirely clear what's being returned. Switching to function is clearer. See: vuejs/pinia#978
It took quite a bit of googling before finding this, so definitely thumbs up.
how does Pinia decide what goes in "state", what goes in "getters", and what goes in "actions". |
is it possable define a async store ? export const useCounterStore = defineStore('counter', async () => {
const count = ref(0)
function increment() {
count.value++
}
return { count, increment }
}) const counterStore = await useCounterStore() |
not possible. |
@Thy3634 What are you trying to accomplish? You can certainly put async functions inside the store, if that helps. |
It does not raise a type error. It tells you the Either the types must be fixed somehow to disallow async setup, or a mention should be put in the docs that it's not supported. As for what I want to accomplish? I want to run an async API call to initialize the store with a value, instead of |
I think I removed the type check because it created other problems. The doc already state that the setup function must return an object. It never shows an example of async setup. If you found that in an article or video, it would be worth letting the author know that is not possible. As for your question, I believe it was asked before in the Discussions, so you should be able to find help there. Long story short, you will have to await for the data to be ready before mounting the component and there are many ways of doing this (before app.mount, in a navigation guard, in a component that is the child of Suspense). Feel free to open a discussion to gather information about this BTW. I'm locking this one as it's outdated now. |
I think that the setup way of building a store will be the future. I would love to shift towards that in the docs.
Currently in the docs
https://pinia.vuejs.org/introduction.html#basic-example
Comprehensive example
Notes
isEven
is a getter without arguments which works similar to a value.messageIfEven(message)
which works more like a method. It takes an argument and also uses the other getterisEven
..value
after values and computed properties by using$ref()
, but now we still has to deal withref()
.I hope this can help someone.
The text was updated successfully, but these errors were encountered: