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
* 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)
- 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
27
</script>
27
28
```
29
+
28
30
Vue 3 exposes Reactivity APIs like `ref` or `computed`, as well as lifecycle hooks and helpers that are auto-imported by Nuxt.
29
31
30
32
## Vue and Nuxt composables
@@ -34,18 +36,23 @@ When you are using the built-in Composition API composables provided by Vue and
34
36
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
37
36
38
1. Permitted Contexts:
37
-
* Nuxt plugins
38
-
* Nuxt route middleware
39
-
* Vue setup functions
39
+
40
+
- Nuxt plugins
41
+
- Nuxt route middleware
42
+
- Vue setup functions
43
+
40
44
2. Synchronous Execution Requirement:
41
-
* Composables must be called synchronously
42
-
* Using await before calling a composable is generally prohibited
45
+
46
+
- Composables must be called synchronously
47
+
- Using await before calling a composable is generally prohibited
48
+
43
49
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`
50
+
Some specialized contexts allow for asynchronous usage while maintaining the synchronous context:
51
+
52
+
-`<script setup>` blocks
53
+
- Components defined with `defineNuxtComponent`
54
+
- Functions created with `defineNuxtPlugin`
55
+
- Route middleware defined with `defineNuxtRouteMiddleware`
49
56
50
57
These limitations are designed to prevent state management issues and ensure clean, predictable component and application behavior during server-side rendering.
51
58
@@ -54,17 +61,18 @@ If you get an error message like `Nuxt instance is unavailable` then it probably
54
61
## Directory-based Auto-imports
55
62
56
63
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.
64
+
65
+
-`components/` for [Vue components](https://nuxt.com/docs/guide/directory-structure/components).
66
+
-`composables/` for [Vue composables](https://nuxt.com/docs/guide/directory-structure/composables).
67
+
-`utils/` for helper functions and other utilities.
60
68
61
69
### Explicit imports
62
70
63
71
Nuxt exposes every auto-import with the `#imports` alias that can be used to make the import explicit if needed.
64
72
65
73
```vue
66
74
<script setup lang="ts">
67
-
import { ref, computed } from '#imports'
75
+
import { computed, ref } from '#imports'
68
76
69
77
const count = ref(1)
70
78
const double = computed(() => count.value * 2)
@@ -88,6 +96,7 @@ This will disable auto-imports completely but it's still possible to use explici
88
96
### Partially Disabling Auto-imports
89
97
90
98
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:
0 commit comments