You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成文件名不含component字样的Angular组件?

Angular CLI生成组件时移除文件名中的"component"后缀

执行Angular CLI的组件生成命令:

ng g c test

或者完整写法:

ng generate component test

默认情况下,Angular会生成带component后缀的组件文件:

CREATE src/app/test/test.component.css (0 bytes)
CREATE src/app/test/test.component.html (19 bytes)
CREATE src/app/test/test.component.spec.ts (612 bytes)
CREATE src/app/test/test.component.ts (267 bytes)

要让文件名不再包含component字样,可以通过配置Angular CLI的schematics实现,具体操作如下:

  • 修改项目根目录的angular.json文件,找到对应项目的配置节点(位于projects字段下)
  • 在该节点的schematics中添加组件生成的自定义规则:
"schematics": {
  "@schematics/angular:component": {
    "fileName": "{{name}}",
    "styleFileExt": "css"
  }
}

这里fileName设为{{name}},意味着直接使用你指定的组件名作为文件名前缀,不再自动追加component后缀。

配置完成后,再次执行组件生成命令,就能得到你期望的文件结构:

CREATE src/app/test/test.css (0 bytes)
CREATE src/app/test/test.html (19 bytes)
CREATE src/app/test/test.spec.ts (612 bytes)
CREATE src/app/test/test.ts (267 bytes)

如果只是临时需要这种命名方式,也可以在命令行直接指定参数:

ng g c test --fileName=test

内容的提问来源于stack exchange,提问作者Hassen Gaaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:54:55