Current color: {{ color }}
89 | 90 | ``` 91 | 92 | 93 | -------------------------------------------------------------------------------- /docs/directory-structure/plugins.md: -------------------------------------------------------------------------------- 1 | # 插件 [plugins](https://v3.nuxtjs.org/docs/directory-structure/plugins) 2 | 3 | # 插件目录 4 | 5 | Nuxt 将自动读取"plugins"目录中的文件并加载它们。如果仅想在服务器端或客户端加载插件时,可以在文件名中使用`.server`或`.client`后缀。 6 | 7 | ::alert{type=warning} 8 | `plugins/`目录中的所有插件都是自动注册的,因此您不应将它们单独添加到"nuxt.config"中。 9 | :: 10 | 11 | ## 创建插件 12 | 13 | 传递给插件的唯一参数是 [`nuxtApp`](https://v3.nuxtjs.org/docs/usage/nuxt-app). 14 | 15 | ```ts 16 | import { defineNuxtPlugin } from "#app" 17 | 18 | export default defineNuxtPlugin((nuxtApp) => { 19 | // Doing something with nuxtApp 20 | }) 21 | ``` 22 | 23 | ## 自动提供 helper 功能 24 | 25 | 如果您想在`NuxtApp`实例上提供 helper 功能,只需在插件中`provide`键值返回即可。例如: 26 | 27 | ```ts 28 | import { defineNuxtPlugin } from "#app" 29 | 30 | export default defineNuxtPlugin(() => { 31 | return { 32 | provide: { 33 | hello: () => "world", 34 | }, 35 | } 36 | }) 37 | ``` 38 | 39 | 在另一个文件中,您可以使用以下内容: 40 | 41 | ```vue 42 | 43 |Loading comments...
115 | 116 |{{ mountain.description }}
135 | 136 | ``` 137 | 138 | 139 | ### 使用async setup 140 | 141 | 受Vue3的影响,如果你使用了`async setup()`的形式,当前组件的实例会在第一个异步操作之后丢失。如果你想要使用多个异步操作,比如执行多个useFetch,你需要使用` 161 | 162 | 163 |{{ organization.description }}
166 |