From 22c3e33c1e92bda333d858c2c5079d74ab9e45e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Ollivier?= Date: Fri, 18 Feb 2022 19:20:55 +0100 Subject: [PATCH] docs: document auto-imports and avoid `#app` and `#imports` in examples (#3296) --- docs/content/1.getting-started/3.bridge.md | 5 +- .../4.bridge-composition-api.md | 37 +++--------- docs/content/2.concepts/4.auto-imports.md | 58 +++++++++++++++++++ .../content/2.concepts/{4.esm.md => 5.esm.md} | 0 .../{5.typescript.md => 6.typescript.md} | 2 +- docs/content/3.docs/1.usage/4.nuxt-app.md | 2 - .../3.docs/2.directory-structure/10.pages.md | 5 +- .../2.directory-structure/11.plugins.md | 5 -- .../2.directory-structure/5.composables.md | 6 +- docs/content/3.docs/4.advanced/2.modules.md | 6 ++ 10 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 docs/content/2.concepts/4.auto-imports.md rename docs/content/2.concepts/{4.esm.md => 5.esm.md} (100%) rename docs/content/2.concepts/{5.typescript.md => 6.typescript.md} (97%) diff --git a/docs/content/1.getting-started/3.bridge.md b/docs/content/1.getting-started/3.bridge.md index 61dd509d63..d6df60a566 100644 --- a/docs/content/1.getting-started/3.bridge.md +++ b/docs/content/1.getting-started/3.bridge.md @@ -136,7 +136,7 @@ You may also need to add `@vue/runtime-dom` as a devDependency if you are strugg :: ::alert Keep in mind that all options extended from `./.nuxt/tsconfig.json` will be overwritten by the options defined in your `tsconfig.json`. -Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead Typescript to not factor in the module resolutions from `./.nuxt/tsconfig.json`. This can lead to module resolutions such as `#app` not being recognized. +Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead Typescript to not factor in the module resolutions from `./.nuxt/tsconfig.json`. This can lead to module resolutions such as `#imports` not being recognized. In case you need to extend options provided by `./.nuxt/tsconfig.json` further, you can use the `alias` property withing your `nuxt.config`. `nuxi` will pick them up and extend `./.nuxt/tsconfig.json` accordingly. :: @@ -184,8 +184,6 @@ You can now migrate to the Nuxt 3 plugins API, which is slightly different in fo Plugins now take only one argument (`nuxtApp`). You can find out more in [the docs](/docs/directory-structure/plugins). ```js -import { defineNuxtPlugin } from '#app' - export default defineNuxtPlugin(nuxtApp => { nuxtApp.provide('injected', () => 'my injected function') // now available on `nuxtApp.$injected` @@ -202,7 +200,6 @@ Nuxt Bridge provides a new Nuxt 3 meta API that can be accessed with a new `useM ```vue -``` - ### `useContext` and `withContext` You can access injected helpers using `useNuxtApp`. ```diff - import { useContext } from '@nuxtjs/composition-api` -+ import { useNuxtApp } from '#app' + const { $axios } = useNuxtApp() ``` -```vue - -``` - ::alert{icon=👉} `useNuxtApp()` also provides a key called `nuxt2Context` which contains all the same properties you would normally access from Nuxt 2 context, but it's advised _not_ to use this directly, as it won't exist in Nuxt 3. Instead, see if there is another way to access what you need. (If not, please raise a feature request or discussion.) :: @@ -213,7 +197,6 @@ Migrating to the new composables from `useAsync`: ```diff +``` + +## Vue auto-imports + +Vue 3 exposes Reactivity APIs like `ref` or `computed`, as well as lifecycle hooks and helpers that are auto-imported by Nuxt. + +```vue + +``` + +## Directory based auto-imports + +Nuxt directly auto-imports files created in defined directories: + +- `components/` for [Vue components](/docs/directory-structure/components). +- `composables/` for [Vue composables](/docs/directory-structure/composables). + +## Explicit imports + +Every Nuxt auto-import is exposed by the `#imports` alias that can be used to make the import explicit if needed: + +```vue + +``` diff --git a/docs/content/2.concepts/4.esm.md b/docs/content/2.concepts/5.esm.md similarity index 100% rename from docs/content/2.concepts/4.esm.md rename to docs/content/2.concepts/5.esm.md diff --git a/docs/content/2.concepts/5.typescript.md b/docs/content/2.concepts/6.typescript.md similarity index 97% rename from docs/content/2.concepts/5.typescript.md rename to docs/content/2.concepts/6.typescript.md index 8e0662f5ab..46e3c07cce 100644 --- a/docs/content/2.concepts/5.typescript.md +++ b/docs/content/2.concepts/6.typescript.md @@ -31,7 +31,7 @@ Nitro also [auto-generates types](/concepts/server-engine#typed-api-routes) for :: ::alert Keep in mind that all options extended from `./.nuxt/tsconfig.json` will be overwritten by the options defined in your `tsconfig.json`. -Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead Typescript to not factor in the module resolutions from `./.nuxt/tsconfig.json`. This can lead to module resolutions such as `#app` not being recognized. +Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead Typescript to not factor in the module resolutions from `./.nuxt/tsconfig.json`. This can lead to module resolutions such as `#imports` not being recognized. In case you need to extend options provided by `./.nuxt/tsconfig.json` further, you can use the `alias` property within your `nuxt.config`. `nuxi` will pick them up and extend `./.nuxt/tsconfig.json` accordingly. :: diff --git a/docs/content/3.docs/1.usage/4.nuxt-app.md b/docs/content/3.docs/1.usage/4.nuxt-app.md index a5b9a63fa4..2ddcd91617 100644 --- a/docs/content/3.docs/1.usage/4.nuxt-app.md +++ b/docs/content/3.docs/1.usage/4.nuxt-app.md @@ -9,8 +9,6 @@ In Nuxt 2, this was referred to as [Nuxt context](https://nuxtjs.org/docs/intern Within composables, plugins and components you can access `nuxtApp` with `useNuxtApp`: ```js -import { useNuxtApp } from '#app' - function useMyComposable () { const nuxtApp = useNuxtApp() // access runtime nuxt app instance diff --git a/docs/content/3.docs/2.directory-structure/10.pages.md b/docs/content/3.docs/2.directory-structure/10.pages.md index dd6f2210f0..574578dfe7 100644 --- a/docs/content/3.docs/2.directory-structure/10.pages.md +++ b/docs/content/3.docs/2.directory-structure/10.pages.md @@ -48,11 +48,8 @@ admins 123 If you want to access the route using Composition API, there is a global `useRoute` function that will allow you to access the route just like `this.$route` in the Options API. -```js +```vue