Javascript 为什么没有 Angular-CLI 为 Angular 项目中的“模型”生成命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53003483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:02:34  来源:igfitidea点击:

Why no Angular-CLI generate command for 'model' in Angular Project?

javascriptangularangular-cli

提问by Deadpool

While learning up Angular I just went through the various Angular-CLI commands to generate individual parts of Angular like 'Component', 'Services', 'Interface', 'Pipes', etc.

在学习 Angular 时,我只是通过各种 Angular-CLI 命令来生成 Angular 的各个部分,如“组件”、“服务”、“接口”、“管道”等。

Generating Angular Items via Angular-CLI

通过 Angular-CLI 生成 Angular 项目

ng g c components/comp-1 //generates component
ng g s services/service-1 // generates service
ng g i interfaces/interface-1 // generates interface

But, I am amazed why there is no generate command for 'Model' (though Interface also does nearly some work -- but model is more powerful as can contain methods also in Class).

但是,我很惊讶为什么没有“模型”的生成命令(尽管接口也做了一些工作——但模型更强大,因为它也可以包含类中的方法)。

Am I missing something or Team-Angular missed on generating a command for 'automatically generating Models' -- as they are at the very core of OOPS Framework.

我是否遗漏了什么或 Team-Angular 在生成“自动生成模型”的命令时遗漏了一些东西——因为它们是 OOPS 框架的核心。

enter image description here

在此处输入图片说明

Reference:

参考:

https://www.npmjs.com/package/angular-cli

https://www.npmjs.com/package/angular-cli

回答by Outman

Because a model isa class, to generate it use --typeoption like this:

因为模型一个类,所以要生成它,请使用如下--type选项:

ng generate class hero --type=model

will result in:

将导致:

hero.model.ts

回答by Nimezzz

You cannot generate a model directly. Actually model is a class. ng generate allows only following types to generate by default.

您不能直接生成模型。实际上模型是一个类。ng generate 默认只允许生成以下类型。

  1. appShell
  2. application
  3. class
  4. component
  5. directive
  6. enum
  7. guard
  8. interface
  9. library
  10. module
  11. pipe
  12. service
  13. serviceWorker
  14. universal
  15. webWorker
  1. 应用程序外壳
  2. 应用
  3. 班级
  4. 成分
  5. 指示
  6. 枚举
  7. 警卫
  8. 界面
  9. 图书馆
  10. 模块
  11. 管道
  12. 服务
  13. 服务工作者
  14. 普遍的
  15. 网络工作者

So in your case you can use --type option to define a generate classes. Let's assume you want a class like bar.foo.ts

因此,在您的情况下,您可以使用 --type 选项来定义生成类。假设你想要一个像 bar.foo.ts 这样的类

You just have to define it with following option.

您只需要使用以下选项定义它。

ng generate class bar --type=foo

in your case you can define a module with this command

在您的情况下,您可以使用此命令定义一个模块

ng generate class nameOfYourModule --type=model

It will generate nameOfYourModule.model.ts

它将生成 nameOfYourModule.model.ts

Please refer this official documentationof ng generate options for more informations.

有关更多信息,请参阅ng generate options 的官方文档

回答by selva kumar

This command also generate model

此命令还生成模型

ng generate class employee --type

ng generate class employee --type

Result In:

导致:

employee.tsemployee.spec.ts

employee.tsemployee.spec.ts