modules
使用 modules/ 目录在你的应用中自动注册本地模块。
它是在构建应用时放置你开发的任何本地模块的理想位置。
¥It is a good place to place any local modules you develop while building your application.
自动注册的文件模式如下:
¥The auto-registered files patterns are:
modules/*/index.ts
modules/*.ts
你无需单独将这些本地模块添加到你的 nuxt.config.ts
中。
¥You don't need to add those local modules to your nuxt.config.ts
separately.
// `nuxt/kit` is a helper subpath import you can use when defining local modules
// that means you do not need to add `@nuxt/kit` to your project's dependencies
import { createResolver, defineNuxtModule, addServerHandler } from 'nuxt/kit'
export default defineNuxtModule({
meta: {
name: 'hello'
},
setup () {
const resolver = createResolver(import.meta.url)
// Add an API route
addServerHandler({
route: '/api/hello',
handler: resolver.resolve('./runtime/api-route')
})
}
})
export default defineEventHandler(() => {
return { hello: 'world' }
})
启动 Nuxt 时,hello
模块将被注册,/api/hello
路由将可用。
¥When starting Nuxt, the hello
module will be registered and the /api/hello
route will be available.
模块按以下顺序执行:
¥Modules are executed in the following sequence:
- 首先,加载
nuxt.config.ts
中定义的模块。 - 然后,执行
modules/
目录中的模块,并按字母顺序加载。
你可以通过在每个目录名称前面添加数字来更改本地模块的顺序:
¥You can change the order of local module by adding a number to the front of each directory name:
Directory structure
modules/
1.first-module/
index.ts
2.second-module.ts