Skip to content

Commit 5e96387

Browse files
Update index.md for auto-imports.
Work on adding documentation for the auto-import functionality/setup/how it works.
1 parent c3f675c commit 5e96387

File tree

1 file changed

+118
-0
lines changed
  • content/2.concepts/4.auto-imports

1 file changed

+118
-0
lines changed

content/2.concepts/4.auto-imports/index.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,121 @@ ogImage: true
33
---
44

55
# Auto Imports
6+
7+
Nuxt auto-imports components, composables and [Vue.js APIs](https://vuejs.org/api/) to use across your application without explicitly importing them.
8+
Because of its opinionated directory structure, Nuxt imports your `components/`, `/compostables` and `/utils`.
9+
10+
In the `server` directory, Nuxt auto-imports exported functions and variables from `server/utils/`.
11+
12+
You are also able to auto-import functions exported deom custom folders or third-party packages by configuring the `imports` section of your `nuxt.config` file.
13+
14+
## Built-in Auto-imports
15+
16+
Nuxt auto-imports functions and compostables to:
17+
* perform [data fetching](https://nuxt.com/docs/getting-started/data-fetching)
18+
* get access to the [app context](https://nuxt.com/docs/api/composables/use-nuxt-app) and [running config](https://nuxt.com/docs/guide/going-further/runtime-config)
19+
* manage [state](https://nuxt.com/docs/getting-started/state-management)
20+
* or define components and plugins
21+
22+
```vue
23+
<script setup lang="ts">
24+
/* useFetch() is auto-imported */
25+
const { data, refresh, status } = await useFetch('/api/hello')
26+
</script>
27+
```
28+
Vue 3 exposes Reactivity APIs like `ref` or `computed`, as well as lifecycle hooks and helpers that are auto-imported by Nuxt.
29+
30+
## Vue and Nuxt composables
31+
32+
When you are using the built-in Composition API composables provided by Vue and Nuxt, be aware that many of them rely on being called in the right <em>context</em>.
33+
34+
The global variable tracking mechanism in Vue and Nuxt imposes strict usage constraints on composables and context-specific functions. These restrictions mean that developers can typically only access these instances within specific, controlled environments:
35+
36+
1. Permitted Contexts:
37+
* Nuxt plugins
38+
* Nuxt route middleware
39+
* Vue setup functions
40+
2. Synchronous Execution Requirement:
41+
* Composables must be called synchronously
42+
* Using await before calling a composable is generally prohibited
43+
3. Exceptions to the Rule:
44+
Some specialized contexts allow for asynchronous usage while maintaining the synchronous context:
45+
* `<script setup>` blocks
46+
* Components defined with `defineNuxtComponent`
47+
* Functions created with `defineNuxtPlugin`
48+
* Route middleware defined with `defineNuxtRouteMiddleware`
49+
50+
These limitations are designed to prevent state management issues and ensure clean, predictable component and application behavior during server-side rendering.
51+
52+
If you get an error message like `Nuxt instance is unavailable` then it probably means you are calling a Nuxt composable in the wrong place in the Vue or Nuxt lifecycle.
53+
54+
## Directory-based Auto-imports
55+
56+
Nuxt directly auto-imports files created in defined directories:
57+
* `components/` for [Vue components](https://nuxt.com/docs/guide/directory-structure/components).
58+
* `composables/` for [Vue composables](https://nuxt.com/docs/guide/directory-structure/composables).
59+
* `utils/` for helper functions and other utilities.
60+
61+
### Explicit imports
62+
63+
Nuxt exposes every auto-import with the `#imports` alias that can be used to make the import explicit if needed.
64+
65+
```vue
66+
<script setup lang="ts">
67+
import { ref, computed } from '#imports'
68+
69+
const count = ref(1)
70+
const double = computed(() => count.value * 2)
71+
</script>
72+
```
73+
74+
### Disabling Auto-imports
75+
76+
If you want to disable auto-importing composables and utlities, you can set `imports.autoImport` to `false` in the `nuxt.config` file.
77+
78+
```ts filename="nuxt.config.ts"
79+
export default defineNuxtConfig({
80+
imports: {
81+
autoImport: false
82+
}
83+
})
84+
```
85+
86+
This will disable auto-imports completely but it's still possible to use explicit imports from `#imports`.
87+
88+
### Partially Disabling Auto-imports
89+
90+
If you want framework-specific functions like `ref` to remain auto-imported but wish to disable auto-imports for your own code (e.g., custom composables), you can set the `imports.scan` option to `false` in your `nuxt.config.ts` file:
91+
```ts filename="nuxt.config.ts"
92+
export default defineNuxtConfig({
93+
imports: {
94+
scan: false
95+
}
96+
})
97+
```
98+
99+
With this configuration framework functions like `ref`, `computed` or `watch` will still work without needing manual imports. Custom code, such as composables, will need to be manually imported in your files.
100+
101+
CAUTION: This setup has certain [limitations](https://nuxt.com/docs/guide/concepts/auto-imports#partially-disabling-auto-imports).
102+
103+
### Auto-imported components
104+
105+
Nuxt automatically imports components from your `~/components` directory. To disable auto-importing components from your own `~/components` directory, you can set `components.dirs` to an empty array
106+
107+
### Auto-import from Third-party packages
108+
109+
Nuxt also allows auto-importing from third-party packages.
110+
For example, you could enable the auto-import of the useI18n composable from the `vue-i18n` package like this:
111+
112+
```ts filename="nuxt.config.ts"
113+
export default defineNuxtConfig({
114+
imports: {
115+
presets: [
116+
{
117+
from: 'vue-i18n',
118+
imports: ['useI18n']
119+
}
120+
]
121+
}
122+
})
123+
```

0 commit comments

Comments
 (0)