nuxt add
在你的 Nuxt 应用中生成一个实体。
Terminal
npx nuxt add <TEMPLATE> <NAME> [--cwd=<directory>] [--logLevel=<silent|info|verbose>] [--force]
参数
Argument | Description |
---|---|
TEMPLATE | 指定要生成的模板(选项:<api|app|app-config|component|composable|error|layer|layout|middleware|module|page|plugin|server-middleware|server-plugin|server-route|server-util>) |
NAME | 指定生成文件的名称 |
选项
Option | Default | Description |
---|---|---|
--cwd=<directory> | . | 指定工作目录 |
--logLevel=<silent|info|verbose> | 指定构建时的日志级别 | |
--force | false | 如果文件已存在则强制覆盖 |
修饰符:
某些模板支持额外的修饰符标志,用于在名称中添加后缀(例如 .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
(可接受connect
、delete
、get
、head
、options
、patch
、post
、put
或trace
)或者你也可以直接使用--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