From 7650f2f31e2962248f1891df0883dd36f865c48f Mon Sep 17 00:00:00 2001 From: David Mears <60350599+david-mears-2@users.noreply.github.com> Date: Fri, 6 Sep 2024 00:00:50 +0100 Subject: [PATCH 1/3] Update nuxt.md Replace tip that was relevant to Nuxt 2 with the equivalent syntax for Nuxt 3. --- packages/docs/ssr/nuxt.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/docs/ssr/nuxt.md b/packages/docs/ssr/nuxt.md index 12c5454e74..1672a34496 100644 --- a/packages/docs/ssr/nuxt.md +++ b/packages/docs/ssr/nuxt.md @@ -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); -export default { - asyncData({ $pinia }) { - const store = useStore($pinia) - }, -} +onMounted(() => { + if (window.innerWidth < 900) { + store.doAction() + } +}); ``` ::: From 986636a7e50866b44c564bdaa148955d0f61b184 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 28 Nov 2024 14:34:56 +0100 Subject: [PATCH 2/3] Apply suggestions from code review [skip ci] --- packages/docs/ssr/nuxt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docs/ssr/nuxt.md b/packages/docs/ssr/nuxt.md index 1672a34496..2be15a2b29 100644 --- a/packages/docs/ssr/nuxt.md +++ b/packages/docs/ssr/nuxt.md @@ -62,7 +62,7 @@ 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()`, for the reasons alluded to [here](https://pinia.vuejs.org/core-concepts/outside-component-usage.html#SSR-Apps). +If you want to use a store outside of `setup()`, remember to pass the `$pinia` instance 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' From 1847d7d0479a3f64d45e7d3694039d2bc6856f37 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 28 Nov 2024 14:47:35 +0100 Subject: [PATCH 3/3] refactor: changes --- packages/docs/ssr/nuxt.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/docs/ssr/nuxt.md b/packages/docs/ssr/nuxt.md index 2be15a2b29..b2793afea5 100644 --- a/packages/docs/ssr/nuxt.md +++ b/packages/docs/ssr/nuxt.md @@ -62,19 +62,18 @@ await useAsyncData('user', () => store.fetchUser().then(() => true)) ::: tip -If you want to use a store outside of `setup()`, remember to pass the `$pinia` instance to `useStore()`, for the reasons alluded to [here](https://pinia.vuejs.org/core-concepts/outside-component-usage.html#SSR-Apps). +If you want to use a store outside of `setup()` or an _injection aware_ context (e.g. Navigation guards, other stores, Nuxt Middlewares, etc), remember to pass the `pinia` instance to `useStore()`, for the reasons alluded to [here](https://pinia.vuejs.org/core-concepts/outside-component-usage.html#SSR-Apps). Retrieving the `pinia` instance might vary. -```js +```ts import { useStore } from '~/stores/myStore' -const store = useStore(useNuxtApp().$pinia); -onMounted(() => { - if (window.innerWidth < 900) { - store.doAction() - } -}); +// this line is usually inside a function that is able to retrieve +// the pinia instance +const store = useStore(pinia) ``` +Fortunately, most of the time you **don't need to go through this hassle**. + ::: ## Auto imports