|
2 | 2 |
|
3 | 3 | Data loaders streamline any asynchronous state management with Vue Router, like **Data Fetching**. Adopting Data loaders ensures a consistent and efficient way to manage data fetching in your application. Keep all the benefits of using libraries like [Pinia Colada](./colada/) or [Apollo](./apollo/) and integrate them seamlessly with client-side navigation.
|
4 | 4 |
|
5 |
| -This is achieved by extracting the loading logic **outside** of the component `setup` (unlike `<Suspense>`). This way, the loading logic can be executed independently of the component lifecycle, and the component can focus on rendering the data. Data Loaders are automatically collected and awaited within a navigation guard, ensuring the data is ready before rendering the component. |
| 5 | +This is achieved by extracting the loading logic **outside** of the component `setup` (unlike `<Suspense>`). This way, the loading logic can be executed independently of the component life cycle, and the component can focus on rendering the data. Data Loaders are automatically collected and awaited within a navigation guard, ensuring the data is ready before rendering the component. |
6 | 6 |
|
7 | 7 | ## Features
|
8 | 8 |
|
@@ -46,9 +46,11 @@ There are different data loaders implementation, the most simple one is the [Bas
|
46 | 46 |
|
47 | 47 | Loaders are [composables](https://vuejs.org/guide/reusability/composables.html) defined through a `defineLoader` function like `defineBasicLoader` or `defineColadaLoader`. They are _used_ in the component `setup` to extract the needed information.
|
48 | 48 |
|
49 |
| -To get started, _define_ and _export_ a loader from a page: |
| 49 | +To get started, _define_ and _**export**_ a loader from a **page** component: |
50 | 50 |
|
51 |
| -```vue{2,5-7,11-16} twoslash |
| 51 | +::: code-group |
| 52 | + |
| 53 | +```vue{2,5-7,11-16} twoslash [src/pages/users/[id].vue] |
52 | 54 | <script lang="ts">
|
53 | 55 | import 'unplugin-vue-router/client'
|
54 | 56 | import './typed-router.d'
|
@@ -84,6 +86,8 @@ const {
|
84 | 86 | </template>
|
85 | 87 | ```
|
86 | 88 |
|
| 89 | +::: |
| 90 | + |
87 | 91 | The loader will automatically run when the route changes, for example when navigating to `/users/1`, even when coming from `/users/2`, the loader will fetch the data and delay the navigation until the data is ready.
|
88 | 92 |
|
89 | 93 | On top of that, you are free to _reuse_ the returned composable `useUserData` in any other component, and it will automatically share the same data fetching instance.
|
|
0 commit comments