-
-
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
Docs: Nuxt 2 --> 3 #2766
base: v2
Are you sure you want to change the base?
Docs: Nuxt 2 --> 3 #2766
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,16 +62,17 @@ await useAsyncData('user', () => store.fetchUser().then(() => true)) | |
|
||
::: tip | ||
|
||
If you want to use a store outside of `setup()`, remember to pass the `pinia` object to `useStore()`. We added it to [the context](https://nuxtjs.org/docs/2.x/internals-glossary/context) so you have access to it in `asyncData()` and `fetch()`: | ||
If you want to use a store outside of `setup()`, remember to pass the `$pinia` object to `useStore()`, for the reasons alluded to [here](https://pinia.vuejs.org/core-concepts/outside-component-usage.html#SSR-Apps). | ||
|
||
```js | ||
import { useStore } from '~/stores/myStore' | ||
const store = useStore(useNuxtApp().$pinia); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still problematic because of teh usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the whole of Nuxt 3 is a place “where the context is not available”, until you call useNuxtApp(), which is the Nuxt 3 equivalent of context https://nuxt.com/docs/guide/going-further/nuxt-app (eta I fixed that link) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then maybe this is just not longer needed, only in some more convoluted examples like lazy invocation in a plugin function nuxtPlugin() {
const nuxt = useNuxtApp()
function something() {
useStore(nuxt.$pinia)
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there someone we can ask? It seems important to clarify, since I read elsewhere in the documentation that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existence of the tip implies to me that the module isn’t automatically passing the pinia instance to the store, except in the setup function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think you are already asking the right person 😅 These parts of the docs could be updated. The different is a little bit more subtle, as it works now inside navigation guards and other parts of the application since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I'm just very confused about all of this. The existing documentation made me believe I needed to do something to prevent Cross-Request State Pollution when using a pinia store inside an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is what my example / diff does. I call useStore (and useNuxtApp) in the setup function, and store.doAction() in the onMounted(). I don't believe I have enough context about Nuxt and Vue to be able to help complete this documentation, but I thank you for your kind responses. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you don't need to pass the pinia instance within setup. I assumed the code sample was outside of |
||
|
||
export default { | ||
asyncData({ $pinia }) { | ||
const store = useStore($pinia) | ||
}, | ||
} | ||
onMounted(() => { | ||
if (window.innerWidth < 900) { | ||
store.doAction() | ||
} | ||
}); | ||
``` | ||
|
||
::: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.