You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/2.concepts/4.auto-imports/index.md
+118Lines changed: 118 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,3 +3,121 @@ ogImage: true
3
3
---
4
4
5
5
# 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.
* 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)
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
+
exportdefaultdefineNuxtConfig({
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
+
exportdefaultdefineNuxtConfig({
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:
0 commit comments