Skip to content

Commit 84818e4

Browse files
committed
docs: must export loader
Close #535 Squashed commit of the following: commit 41d1780 Author: Eduardo San Martin Morote <[email protected]> Date: Mon Jan 27 10:10:35 2025 +0100 docs: reword commit 2b9cd36 Author: Eduardo San Martin Morote <[email protected]> Date: Mon Jan 27 10:04:38 2025 +0100 docs: reword commit c709249 Author: Rick Tibbe <[email protected]> Date: Tue Nov 5 12:50:24 2024 +0100 docs: add information on using loaders outside of page components
1 parent dd25f59 commit 84818e4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

docs/data-loaders/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ app.mount('#app')
4444

4545
There are different data loaders implementation, the most simple one is the [Basic Loader](./basic/) which always reruns data fetching. A more efficient one, is the [Colada Loader](./colada/) which uses [@pinia/colada](https://github.com/posva/pinia-colada) under the hood. In the following examples, we will be using the _basic loader_.
4646

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.
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.
4848

4949
To get started, _define_ and _**export**_ a loader from a **page** component:
5050

@@ -90,7 +90,7 @@ const {
9090

9191
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.
9292

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.
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. You can even [organize your loaders in separate files](./organization.md) as long as you **export** the loader from a **page** component.
9494

9595
## Why Data Loaders?
9696

docs/data-loaders/organization.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ When using this pattern, remember to **export the loader** in all the page compo
4747

4848
## Usage outside of page components
4949

50-
Until now, we have only seen loaders used in page components. However, one of the benefits of using loaders is that they can be **reused in many part of your application**, just like regular composables. This will not only eliminate code duplication but also ensure an optimal and performant data fetching by **deduplicating requests and sharing the data**.
50+
Until now, we have only seen loaders used in page components. However, one of the benefits of using loaders is that they can be **reused in many parts of your application**, just like regular composables. This will not only eliminate code duplication but also ensure an optimal and performant data fetching by **deduplicating requests and sharing the data**.
5151

5252
To use a loader outside of a page component, you can simply **import it** and use it like any other composable, without the need to export it.
5353

@@ -60,6 +60,12 @@ const { data: issues } = useProjectIssues()
6060
</script>
6161
```
6262

63+
::: tip
64+
65+
When using a loader in a non-page component, you must **export the loader** from the page components where it is used. If you only import and use the loader in a regular component, the router will not recognize it and won't trigger or await it during navigation.
66+
67+
:::
68+
6369
## Nested Routes
6470

6571
When defining nested routes, you don't need to worry about exporting the loader in both the parent and the child components. This will be automatically optimized for you and the loader will be shared between the parent and the child components.

0 commit comments

Comments
 (0)