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

Angular17中如何生成单TS文件的独立组件而非四个文件?

Angular CLI 自定义组件生成文件的方法

完全可以通过Angular CLI的参数直接控制生成组件时的文件,不需要依赖编辑器模板。

如果只想生成单个TS文件的独立组件,你可以在命令里添加以下参数:

  • --skip-template:不生成HTML模板文件
  • --skip-style:不生成样式文件
  • --skip-tests:不生成spec测试文件

完整命令如下:

ng g c --standalone --skip-template --skip-style --skip-tests AComponent

如果希望每次生成组件都默认使用这些设置,不用每次手动加参数,可以修改项目根目录下的angular.json文件,找到schematics节点,配置@schematics/angular:component的默认参数:

"schematics": {
  "@schematics/angular:component": {
    "skipTemplate": true,
    "skipStyle": true,
    "skipTests": true,
    "standalone": true
  }
}

配置完成后,直接执行ng g c AComponent就会自动生成仅包含TS文件的独立组件。

内容的提问来源于stack exchange,提问作者M.H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 00:44:50