如何使用 Angula2 CLI 在特定文件夹中生成 类?
How to generate your classes in a specific folder with Angula2 CLI?
我通常使用 angular 2 cli 使用以下命令生成 classes:
ng g class todo
如何让 cli 在特定文件夹(例如模型文件夹)中生成 classes。
文档中有一个示例,其中他们在特定文件夹中生成了一个组件,但我使用 class 选项没有成功
我尝试了以下选项:
ng g class models/todo
ng g class ../models todo
ng g class models todo
它们导致路径无效的错误,这让我认为支持该选项但我无法弄清楚,或者在后者的情况下将预期的文件夹名称与 class名字.
ng generate component my-new-component
ng g component my-new-component # using the alias
组件支持相对路径生成
如果在目录 src/app/feature/
中并且你 运行
ng g component new-cmp
您的组件将在 src/app/feature/new-cmp
中生成
但是如果你运行
ng g component ../newer-cmp
您的组件将在 src/app/newer-cmp
中生成
也适用于 类
为 Angular CLI 生成的项目是相对的...
如果您在根目录(或源目录)中,您将被认为在应用程序目录中...
~/me/foo > ng g c bar
// ~/me/foo/src/app/bar/bar.component*
~/me/foo/src > ng g c bar
// ~/me/foo/src/app/bar/bar.component*
如果您在应用程序目录中或文件夹结构的更下方,您将生成到您所在的位置...
~/me/foo/src/app/feature > ng g c bar
// ~/me/foo/src/app/feature/bar/bar.component*
~/me/foo/src/app/feature > ng g c ../bar
// ~/me/foo/src/app/bar/bar.component*
注意:如果尝试生成一个不存在的目录,会报错...
~/me/foo/src/app > ng g c a/b/c/d/e/f/g/bar
// Error
要在项目的模型文件夹中创建待办事项组件,只需确保您位于项目的根文件夹中并键入:
ng g c /models/todo
此外,如果要指定声明模块的文件名,可以使用 -m 选项。
例如,如果您有一个模型模块,并且想要更新该模块以声明您的新待办事项组件:
ng g c /models/todo -m model
此致
ng g cl model/UserInfo
works fine in angular5
添加到 Karthikeyan Karunanithi 简单答案,
ng g i model/UserInfo
如果你想要一个界面,效果很好...
ng g cl models//todo-class
结果~/project/src/app/models/todo-class.ts
export class TodoClass {}
https://github.com/angular/angular-cli#generating-components-directives-pipes-and-services
生成组件,类,指令,守卫,接口,枚举,管道和服务,模块,图书馆
您可以使用 ng generate(或仅 ng g)命令生成 Angular 个工件:
ng generate component my-new-component
ng g component my-new-component # using the alias
组件支持相对路径生成
如果在目录 src/app/feature/ 中并且你 运行
ng g component new-cmp
你的组件将在src/app/feature/new-cmp
中生成
但是如果你运行
ng g component ./newer-cmp
您的组件将在 src/app/newer-cmp
中生成
如果在目录src/app中你也可以运行
ng g component feature/new-cmp
你的组件将在 src/app/feature/new-cmp
中生成
您可以在下面的 table 中找到所有可能的蓝图:
Scaffold Usage
Component ng g component my-new-component
Directive ng g directive my-new-directive
Pipe ng g pipe my-new-pipe
Service ng g service my-new-service
**Class ng g class my-new-class**
Guard ng g guard my-new-guard
Interface ng g interface my-new-interface
Enum ng g enum my-new-enum
Module ng g module my-module
angular-cli 将在 app.module.ts 中自动添加对组件、指令和管道的引用。如果您需要将此引用添加到另一个自定义模块,请按照以下步骤操作:
ng g module new-module
创建一个新模块
打电话
ng g component new-module/new-component
这应该将新组件、指令或管道引用添加到您创建的新模块。
用于在 Angular 4/5/6
中生成 class
只需右键单击要在其中创建 class 和 select 文件夹 在终端中打开
然后在终端内使用
ng g class classname --type=model
我通常使用 angular 2 cli 使用以下命令生成 classes:
ng g class todo
如何让 cli 在特定文件夹(例如模型文件夹)中生成 classes。
文档中有一个示例,其中他们在特定文件夹中生成了一个组件,但我使用 class 选项没有成功
我尝试了以下选项:
ng g class models/todo
ng g class ../models todo
ng g class models todo
它们导致路径无效的错误,这让我认为支持该选项但我无法弄清楚,或者在后者的情况下将预期的文件夹名称与 class名字.
ng generate component my-new-component
ng g component my-new-component # using the alias
组件支持相对路径生成
如果在目录 src/app/feature/
中并且你 运行
ng g component new-cmp
您的组件将在 src/app/feature/new-cmp
但是如果你运行
ng g component ../newer-cmp
您的组件将在 src/app/newer-cmp
也适用于 类
为 Angular CLI 生成的项目是相对的...
如果您在根目录(或源目录)中,您将被认为在应用程序目录中...
~/me/foo > ng g c bar
// ~/me/foo/src/app/bar/bar.component*
~/me/foo/src > ng g c bar
// ~/me/foo/src/app/bar/bar.component*
如果您在应用程序目录中或文件夹结构的更下方,您将生成到您所在的位置...
~/me/foo/src/app/feature > ng g c bar
// ~/me/foo/src/app/feature/bar/bar.component*
~/me/foo/src/app/feature > ng g c ../bar
// ~/me/foo/src/app/bar/bar.component*
注意:如果尝试生成一个不存在的目录,会报错...
~/me/foo/src/app > ng g c a/b/c/d/e/f/g/bar
// Error
要在项目的模型文件夹中创建待办事项组件,只需确保您位于项目的根文件夹中并键入:
ng g c /models/todo
此外,如果要指定声明模块的文件名,可以使用 -m 选项。
例如,如果您有一个模型模块,并且想要更新该模块以声明您的新待办事项组件:
ng g c /models/todo -m model
此致
ng g cl model/UserInfo
works fine in angular5
添加到 Karthikeyan Karunanithi 简单答案,
ng g i model/UserInfo
如果你想要一个界面,效果很好...
ng g cl models//todo-class
结果~/project/src/app/models/todo-class.ts
export class TodoClass {}
https://github.com/angular/angular-cli#generating-components-directives-pipes-and-services
生成组件,类,指令,守卫,接口,枚举,管道和服务,模块,图书馆
您可以使用 ng generate(或仅 ng g)命令生成 Angular 个工件:
ng generate component my-new-component
ng g component my-new-component # using the alias
组件支持相对路径生成
如果在目录 src/app/feature/ 中并且你 运行
ng g component new-cmp
你的组件将在src/app/feature/new-cmp
中生成但是如果你运行
ng g component ./newer-cmp
您的组件将在 src/app/newer-cmp
中生成如果在目录src/app中你也可以运行
ng g component feature/new-cmp
你的组件将在 src/app/feature/new-cmp
中生成您可以在下面的 table 中找到所有可能的蓝图:
Scaffold Usage
Component ng g component my-new-component
Directive ng g directive my-new-directive
Pipe ng g pipe my-new-pipe
Service ng g service my-new-service
**Class ng g class my-new-class**
Guard ng g guard my-new-guard
Interface ng g interface my-new-interface
Enum ng g enum my-new-enum
Module ng g module my-module
angular-cli 将在 app.module.ts 中自动添加对组件、指令和管道的引用。如果您需要将此引用添加到另一个自定义模块,请按照以下步骤操作:
ng g module new-module
创建一个新模块
打电话
ng g component new-module/new-component
这应该将新组件、指令或管道引用添加到您创建的新模块。
用于在 Angular 4/5/6
中生成 class只需右键单击要在其中创建 class 和 select 文件夹 在终端中打开
然后在终端内使用
ng g class classname --type=model