mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-19 15:10:58 +00:00
docs: add information on how to use options api asyncData (#7019)
This commit is contained in:
parent
2d071eb48c
commit
0087e7bbe0
@ -173,6 +173,27 @@ const refresh = () => refreshNuxtData('count')
|
|||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Options API support
|
||||||
|
|
||||||
|
Nuxt 3 provides a way to perform `asyncData` fetching within the Options API. You must wrap your component definition within `defineNuxtComponent` for this to work.
|
||||||
|
|
||||||
|
```vue
|
||||||
|
<script>
|
||||||
|
export default defineNuxtComponent({
|
||||||
|
fetchKey: 'hello',
|
||||||
|
async asyncData () {
|
||||||
|
return {
|
||||||
|
hello: await $fetch('/api/hello')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
::alert{type=warning}
|
||||||
|
Options API support for `asyncData` may well change before the stable release of Nuxt 3.
|
||||||
|
::
|
||||||
|
|
||||||
## Isomorphic `fetch` and `$fetch`
|
## Isomorphic `fetch` and `$fetch`
|
||||||
|
|
||||||
When we call `fetch` in the browser, user headers like `cookie` will be directly sent to the API. But during server-side-rendering, since the `fetch` request takes place 'internally' within the server, it doesn't include the user's browser cookies, nor does it pass on cookies from the fetch response.
|
When we call `fetch` in the browser, user headers like `cookie` will be directly sent to the API. But during server-side-rendering, since the `fetch` request takes place 'internally' within the server, it doesn't include the user's browser cookies, nor does it pass on cookies from the fetch response.
|
||||||
|
@ -171,6 +171,14 @@ describe('head tags', () => {
|
|||||||
// })
|
// })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('legacy async data', () => {
|
||||||
|
it('should work with defineNuxtComponent', async () => {
|
||||||
|
const html = await $fetch('/legacy/async-data')
|
||||||
|
expect(html).toContain('<div>Hello API</div>')
|
||||||
|
expect(html).toContain('{hello:"Hello API"}')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('navigate', () => {
|
describe('navigate', () => {
|
||||||
it('should redirect to index with navigateTo', async () => {
|
it('should redirect to index with navigateTo', async () => {
|
||||||
const { headers } = await fetch('/navigate-to/', { redirect: 'manual' })
|
const { headers } = await fetch('/navigate-to/', { redirect: 'manual' })
|
||||||
|
15
test/fixtures/basic/pages/legacy/async-data.vue
vendored
Normal file
15
test/fixtures/basic/pages/legacy/async-data.vue
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
{{ hello }}
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default defineNuxtComponent({
|
||||||
|
async asyncData () {
|
||||||
|
return {
|
||||||
|
hello: await $fetch('/api/hello')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
Loading…
Reference in New Issue
Block a user