angular 7 cli在自己的目录中生成每个服务,管道等 [英] angular 7 cli generate each service, pipe,etc in their own directory

查看:140
本文介绍了angular 7 cli在自己的目录中生成每个服务,管道等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Angular Cli的早期版本运行 ng g service services/MyService 时,它会创建:

With early versions of the Angular Cli when I run ng g service services/MyService it created:

services/my-service/my-service.service.ts
services/my-service/my-service.service.spec.ts

但是现在它创建了

services/my-service.service.ts
services/my-service.service.spec.ts

有没有一种方法可以不返回冗长的 ng g service services/my-service/MyService而返回其他行为?我没有发现任何相关内容,但也许我没有使用正确的方法关键字.

Is there a way to go back to the other behavior without write a verbose ng g service services/my-service/MyService ?I had not found anything related but maybe I am not using the correct keywords.

推荐答案

尽管每次执行 ng generate 时都可以传递-flat = false 是根据服务/管道/指令名称创建的,您实际上可以在项目级别的 angular.json 中覆盖默认的逻辑示意图选项,例如 flat ,以避免需要传递每次在命令行上使用-flat = false 选项.例如,要在执行 ng g service services/MyService 时将 flat 设置为 false ,则需要添加一个附加属性 @schematics/angular:service ,位于 angular.json 中的相应项目的 schematics 属性中:

While you can pass --flat=false each time you execute ng generate so that a directory is created based on the service/pipe/directive name, you can actually override default schematics options such as flat at the project level in angular.json to avoid needing to pass the --flat=false option every time on the command line. For example, to set flat to false when executing ng g service services/MyService, you would add an additional property, @schematics/angular:service, in the schematics property of the respective project in angular.json:

...
"projects": {
    "sample-angular": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:service": {
          "flat": false
        }
     },
     ...
}

添加此替代项后,运行命令 ng g service services/MyService ,将产生以下输出:

After adding this override, running the command ng g service services/MyService, generates the following output:

CREATE src/app/services/my-service/my-service.service.spec.ts (349 bytes)
CREATE src/app/services/my-service/my-service.service.ts (138 bytes)

您可以覆盖所需的任何特定原理图,无论是管道,服务,组件,模块还是指令.您可以在/node_modules/@angular/cli/lib/config/schema.json 中查看默认的逻辑示意图选项.有很多选项,您可以精确调整要生成的内容以及如何避免需要记住并将选项传递给命令行的方法.

You can override any specific schematics you need whether that is for Pipes, Services, Components, Modules, or Directives. You can see the default schematics options at /node_modules/@angular/cli/lib/config/schema.json. There are a number of options and you can fine tune exactly what you want generated and how to avoid needing to remember and pass options to the command line.

如果您有多个项目,则可以在与 projects 相同的级别上创建属性 schematics ,以覆盖所有项目的逻辑示意图选项.

If you have multiple projects, you can create a property schematics at the same level as projects to override schematic options for all projects.

希望有帮助!

这篇关于angular 7 cli在自己的目录中生成每个服务,管道等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆