Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

docs: document auto-imports and avoid #app and #imports in examples #3296

Merged
merged 11 commits into from
Feb 18, 2022
Next Next commit
doc(aliases): change #app to #imports in snippets
  • Loading branch information
clemcode committed Feb 17, 2022
commit 3f25fda9cac56d3cb0315740d1872ff832927213
6 changes: 3 additions & 3 deletions docs/content/1.getting-started/3.bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
::
Expand Down Expand Up @@ -184,7 +184,7 @@ 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'
import { defineNuxtPlugin } from '#imports'

export default defineNuxtPlugin(nuxtApp => {
nuxtApp.provide('injected', () => 'my injected function')
Expand All @@ -202,7 +202,7 @@ Nuxt Bridge provides a new Nuxt 3 meta API that can be accessed with a new `useM

```vue
<script setup>
import { useMeta } from '#app'
import { useMeta } from '#imports'
useMeta({
title: 'My Nuxt App',
})
Expand Down
22 changes: 11 additions & 11 deletions docs/content/1.getting-started/4.bridge-composition-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ This function has been removed, but its use cases can be met by using `useNuxtAp

```diff
- import { onGlobalSetup } from '@nuxtjs/composition-api'
+ import { defineNuxtPlugin } from '#app'
+ import { defineNuxtPlugin } from '#imports'

- export default () => {
- onGlobalSetup(() => {
Expand All @@ -116,7 +116,7 @@ The key differences are that you must provide a _key_ for this state (which prev

```diff
- import { ssrRef } from '@nuxtjs/composition-api'
+ import { useState } from '#app'
+ import { useState } from '#imports'

- const ref1 = ssrRef('initialData')
- const ref2 = ssrRef(() => 'factory function')
Expand Down Expand Up @@ -157,13 +157,13 @@ In order to access Vuex store instance, you can use `useNuxtApp().$store`.

```diff
- import { useStore } from '@nuxtjs/composition-api`
+ import { useNuxtApp } from '#app'
+ import { useNuxtApp } from '#imports'
+ const { $store } = useNuxtApp()
```

```vue
<script>
import { useNuxtApp } from '#app'
import { useNuxtApp } from '#imports'
const { $store } = useNuxtApp()
</script>
```
Expand All @@ -174,13 +174,13 @@ You can access injected helpers using `useNuxtApp`.

```diff
- import { useContext } from '@nuxtjs/composition-api`
+ import { useNuxtApp } from '#app'
+ import { useNuxtApp } from '#imports'
+ const { $axios } = useNuxtApp()
```

```vue
<script>
import { useNuxtApp } from '#app'
import { useNuxtApp } from '#imports'
const { $axios } = useNuxtApp()
</script>
```
Expand Down Expand Up @@ -213,7 +213,7 @@ Migrating to the new composables from `useAsync`:
```diff
<script setup>
- import { useAsync } from '@nuxtjs/composition-api'
+ import { useLazyAsyncData, useLazyFetch } from '#app'
+ import { useLazyAsyncData, useLazyFetch } from '#imports'
- const posts = useAsync(() => $fetch('/api/posts'))
+ const { data: posts } = useLazyAsyncData('posts', () => $fetch('/api/posts'))
+ // or, more simply!
Expand All @@ -226,7 +226,7 @@ Migrating to the new composables from `useFetch`:
```diff
<script setup>
- import { useFetch } from '@nuxtjs/composition-api'
+ import { useLazyAsyncData, useLazyFetch } from '#app'
+ import { useLazyAsyncData, useLazyFetch } from '#imports'
- const posts = ref([])
- const { fetch } = useFetch(() => { posts.value = await $fetch('/api/posts') })
+ const { data: posts, refresh } = useLazyAsyncData('posts', () => $fetch('/api/posts'))
Expand All @@ -246,7 +246,7 @@ In order to interact with `vue-meta`, you may use `useNuxt2Meta`, which will wor
```diff
<script setup>
- import { useMeta } from '@nuxtjs/composition-api'
+ import { useNuxt2Meta } from '#app'
+ import { useNuxt2Meta } from '#imports'
useNuxt2Meta({
title: 'My Nuxt App',
})
Expand All @@ -257,7 +257,7 @@ You can also pass in computed values or refs, and the meta values will be update

```ts
<script setup>
import { useNuxt2Meta } from '#app'
import { useNuxt2Meta } from '#imports'
const title = ref('my title')
useNuxt2Meta({
title,
Expand All @@ -275,7 +275,7 @@ Nuxt Bridge also provides a Nuxt 3-compatible meta implementation that can be ac
```diff
<script setup>
- import { useMeta } from '@nuxtjs/composition-api'
+ import { useMeta } from '#app'
+ import { useMeta } from '#imports'
useMeta({
title: 'My Nuxt App',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/content/2.concepts/5.typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
::
Expand Down
2 changes: 2 additions & 0 deletions docs/content/3.docs/1.usage/3.meta-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Within your `setup` function, you can call `useMeta` with an object of meta prop
For example:

```js
import { useMeta } from '#imports'

export default {
setup () {
useMeta({
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.docs/1.usage/4.nuxt-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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'
import { useNuxtApp } from '#imports'

function useMyComposable () {
const nuxtApp = useNuxtApp()
Expand Down
2 changes: 2 additions & 0 deletions docs/content/3.docs/1.usage/5.runtime-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ If you want to use the runtime confg within any (custom) plugin, you can use `us
For example:

```ts
import { defineNuxtPlugin, useRuntimeConfig } from '#imports';

export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig();

Expand Down
2 changes: 2 additions & 0 deletions docs/content/3.docs/1.usage/6.cookies.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The example below creates a cookie called `counter`. If the cookie doesn't exist
</template>

<script setup>
import { useCookie } from '#imports'

const counter = useCookie('counter')
counter.value = counter.value || Math.round(Math.random() * 1000)
</script>
Expand Down
6 changes: 3 additions & 3 deletions docs/content/3.docs/2.directory-structure/11.plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Only `myPlugin.ts` and `myOtherPlugin/index.ts` would be registered.
The only argument passed to a plugin is [`nuxtApp`](/docs/usage/nuxt-app).

```ts
import { defineNuxtPlugin } from '#app'
import { defineNuxtPlugin } from '#imports'

export default defineNuxtPlugin(nuxtApp => {
// Doing something with nuxtApp
Expand All @@ -46,7 +46,7 @@ export default defineNuxtPlugin(nuxtApp => {
If you would like to provide a helper on the `NuxtApp` instance, just return it from the plugin under a `provide` key. For example:

```ts
import { defineNuxtPlugin } from '#app'
import { defineNuxtPlugin } from '#imports'

export default defineNuxtPlugin(() => {
return {
Expand Down Expand Up @@ -115,7 +115,7 @@ yarn add --dev vue-gtag-next
Then create a plugin file `plugins/vue-gtag.client.js`.

```ts
import { defineNuxtPlugin } from '#app'
import { defineNuxtPlugin } from '#imports'
import VueGtag from 'vue-gtag-next'

export default defineNuxtPlugin((nuxtApp) => {
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.docs/2.directory-structure/5.composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Only `useFoo.ts` and `useBar/index.ts` would be searched for imports - and if th
## Example: (using named export)

```js [composables/useFoo.ts]
import { useState } from '#app'
import { useState } from '#imports'

export const useFoo = () => {
return useState('foo', () => 'bar')
Expand All @@ -37,7 +37,7 @@ export const useFoo = () => {
## Example: (using default export)

```js [composables/use-foo.ts or composables/useFoo.ts]
import { useState } from '#app'
import { useState } from '#imports'

// It will be available as useFoo() (camelCase of file name without extension)
export default function () {
Expand Down
2 changes: 2 additions & 0 deletions docs/content/3.docs/2.directory-structure/6.layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ You can also use a ref or computed property for your layout.
</template>

<script setup>
import { useRoute } from '#imports';

const route = useRoute()
function enableCustomLayout () {
route.meta.layout = "custom"
Expand Down
2 changes: 2 additions & 0 deletions docs/content/3.docs/2.directory-structure/7.middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ It is advised to use the helper functions above for performing redirects or stop
It is possible to add global or named route middleware manually using the `addRouteMiddleware()` helper function, such as from within a plugin.

```ts
import { defineNuxtPlugin } from '#imports';
pi0 marked this conversation as resolved.
Show resolved Hide resolved

export default defineNuxtPlugin(() => {
addRouteMiddleware('global-test', () => {
console.log('this global middleware was added in a plugin and will be run on every route change')
Expand Down
6 changes: 6 additions & 0 deletions docs/content/3.docs/4.advanced/2.modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export default defineNuxtConfig({
Creating Nuxt modules involves tedious and common tasks. [Nuxt Kit](/docs/advanced/kit), provides a convenient and standard API to define Nuxt modules using `defineNuxtModule`:

```js
import { defineNuxtModule } from '@nuxt/kit';
pi0 marked this conversation as resolved.
Show resolved Hide resolved

export default defineNuxtModule({
meta: {
// Usually npm package name of your module
Expand Down Expand Up @@ -190,6 +192,8 @@ export default defineNuxtModule<ModuleOptions>({
If your module will provide a CSS library, make sure to check if the user already included the library to avoid duplicates and add an option to disable the CSS library in the module.

```ts
import { defineNuxtModule } from '@nuxt/kit';
pi0 marked this conversation as resolved.
Show resolved Hide resolved

export default defineNuxtModule({
setup (options, nuxt) {
nuxt.options.css.push('font-awesome/css/font-awesome.css')
Expand All @@ -202,6 +206,8 @@ export default defineNuxtModule({
If your module opens handles or starts a watcher, we should close it when the nuxt lifecycle is done. For this, we can use the `close` hook:

```ts
import { defineNuxtModule } from '@nuxt/kit';
pi0 marked this conversation as resolved.
Show resolved Hide resolved

export default defineNuxtModule({
setup (options, nuxt) {
nuxt.hook('close', async nuxt => {
Expand Down