【问题标题】:Generating Components without *.spec.ts in Angular 6在 Angular 6 中生成没有 *.spec.ts 的组件
【发布时间】:2022-02-22 23:39:17
【问题描述】:

在以前的版本中,可以使用 .angular-cli.json 禁用 spec.ts,如下所示。 6.0.0 版本中的 angular.json 有什么办法吗?

"defaults": {
    "component": { 
        "spec": false 
    },
    "service": { 
        "spec": false 
    },
    ...
}

【问题讨论】:

标签: angular6


【解决方案1】:

在“6.0.0”版本中,*.spec.ts 可以用 angular.json 禁用

注意:不要忘记将“prefix”属性值“fmyp”和“fmp”更改为您的。

"schematics": {
    "@schematics/angular:component": {
      "prefix": "fmyp",
      "styleext": "css",
      "spec": false
    },
    "@schematics/angular:directive": {
      "prefix": "fmp",
      "spec": false
    },
    "@schematics/angular:module": {
      "spec": false
    },
    "@schematics/angular:service": {
      "spec": false
    },
    "@schematics/angular:pipe": {
      "spec": false
    },
    "@schematics/angular:class": {
      "spec": false
    }
  }

【讨论】:

  • 请注意,从 Angular 9 开始,此选项已重命名为 "skipTests": true。进行 ng 更新时,它应该会自动更正此问题
【解决方案2】:

简单的方法是您可以像使用以前版本一样使用 CLI。

新语法是ng generate component component-name --skipTests=true

这将创建没有 .spec 文件的组件。 快乐编码:)

【讨论】:

    【解决方案3】:

    方法一:

    您还可以通过添加“--no-spec”在使用 Angular-cli 创建事物时禁用规范生成

    ng generate component my-component --no-spec
    

    方法二: 在 angular.json 文件中永久禁用。您可以编辑项目的原理图。

    "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss",
          "spec": false
        },
        "@schematics/angular:class": {
          "spec": false
        },
        "@schematics/angular:directive": {
          "spec": false
        },
        "@schematics/angular:guard": {
          "spec": false
        },
        "@schematics/angular:module": {
          "spec": false
        },
        "@schematics/angular:pipe": {
          "spec": false
        },
        "@schematics/angular:service": {
          "spec": false
        }
      },
    

    【讨论】:

    • --no-spec 选项是否记录在任何地方?在ng g c 文档或命令行中的ng g c --help 中找不到它。
    • 使用--skipTests=true生成无规格文件
    【解决方案4】:

    // for ealier versions
    ng generate component path/filename --no-spec
    // Or
    ng generate component path/filename --spec false
    
    // for later versions
    ng generate component path/filename --skipTests=true
    // Note: path is relative to src/app folder

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 2016-09-26
      • 2017-04-06
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多