Skip to content

Commit

Permalink
docs: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 24, 2023
1 parent a787bfa commit 75f5c51
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
32 changes: 23 additions & 9 deletions packages/docs/ssr/nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,28 @@ export default defineNuxtConfig({

And that's it, use your store as usual!

## Using the store outside of `setup()`
## Awaiting for actions in pages

As with `onServerPrefetch()`, you can call a store action within `asyncData()`. Given how `useASyncData()` works, **make sure to return a value**. This will allow Nuxt to skip running the action on the client side and reuse the value from the server.

```vue{3-4}
<script setup>
const store = useStore()
// we could also extract the data, but it's already present in the store
await useAsyncData('user', () => store.fetchUser())
</script>
```

If your action doesn't resolve a value, you can add any non nullish value:

```vue{3}
<script setup>
const store = useStore()
await useAsyncData('user', () => store.fetchUser().then(() => true))
</script>
```

::: 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()`:

Expand All @@ -50,14 +71,7 @@ export default {
}
```

As with `onServerPrefetch()`, you don't need to do anything special if you want to call a store action within `asyncData()`:

```vue
<script setup>
const store = useStore()
const { data } = await useAsyncData('user', () => store.fetchUser())
</script>
```
:::

## Auto imports

Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt/playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const counter = useCounter()
useTestStore()
useSomeStoreStore()
await useAsyncData('counter', () => counter.asyncIncrement().then(() => true))
if (process.server) {
counter.increment()
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default defineNuxtConfig({
vite: {
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
__TEST__: 'false',
__USE_DEVTOOLS__: true,
__TEST__: false,
},
},
})
9 changes: 9 additions & 0 deletions packages/nuxt/playground/stores/counter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

export const useCounter = defineStore('counter', {
state: () => ({
count: 100,
Expand All @@ -6,6 +8,13 @@ export const useCounter = defineStore('counter', {
increment() {
this.count += 1
},

async asyncIncrement() {
console.log('asyncIncrement called')
await sleep(300)
this.count++
return true
},
},
getters: {
getCount: (state) => state.count,
Expand Down

0 comments on commit 75f5c51

Please sign in to comment.