nuxt add

源码
在你的 Nuxt 应用中生成一个实体。
Terminal
npx nuxt add <TEMPLATE> <NAME> [--cwd=<directory>] [--logLevel=<silent|info|verbose>] [--force]

参数

ArgumentDescription
TEMPLATE指定要生成的模板(选项:<api|app|app-config|component|composable|error|layer|layout|middleware|module|page|plugin|server-middleware|server-plugin|server-route|server-util>)
NAME指定生成文件的名称

选项

OptionDefaultDescription
--cwd=<directory>.指定工作目录
--logLevel=<silent|info|verbose>指定构建时的日志级别
--forcefalse如果文件已存在则强制覆盖

修饰符:

某些模板支持额外的修饰符标志,用于在名称中添加后缀(例如 .client.get)。

Terminal
# 生成 `/plugins/sockets.client.ts`
npx nuxt add plugin sockets --client

nuxt add component

  • 修饰符标志:--mode client|server--client--server
Terminal
# 生成 `app/components/TheHeader.vue`
npx nuxt add component TheHeader

nuxt add composable

Terminal
# 生成 `app/composables/foo.ts`
npx nuxt add composable foo

nuxt add layout

Terminal
# 生成 `app/layouts/custom.vue`
npx nuxt add layout custom

nuxt add plugin

  • 修饰符标志:--mode client|server--client--server
Terminal
# 生成 `app/plugins/analytics.ts`
npx nuxt add plugin analytics

nuxt add page

Terminal
# 生成 `app/pages/about.vue`
npx nuxt add page about
Terminal
# 生成 `app/pages/category/[id].vue`
npx nuxt add page "category/[id]"

nuxt add middleware

  • 修饰符标志:--global
Terminal
# 生成 `app/middleware/auth.ts`
npx nuxt add middleware auth

nuxt add api

  • 修饰符标志:--method(可接受 connectdeletegetheadoptionspatchpostputtrace)或者你也可以直接使用 --get--post 等。
Terminal
# 生成 `server/api/hello.ts`
npx nuxt add api hello

nuxt add layer

Terminal
# 生成 `layers/subscribe/nuxt.config.ts`
npx nuxt add layer subscribe