`ng generate component/directive --module` declares a class that doesn't exist when `addTypeToClassName` is `false`
维护者通常 1 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- angular, typescript
调研方向
Start with packages/schematics/angular/utility/add-declaration-to-ng-module.ts and compare how the component and directive schematics compute class names in their index.ts files. Reproduce with the ng generate component widget --module app command and ng build; done means the NgModule declaration and export names match the generated class when addTypeToClassName is false. A linked pull request is already open.
由索引模型根据 Issue 内容生成。
描述
Command
generate
Is this a regression?
- Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
The addTypeToClassName option was added in 21.0.0, and the declaration code was not updated, so it never worked.
Description
When a component or directive is generated into an NgModule with addTypeToClassName: false and a type set, the
generated class and the symbol added to the NgModule don't match:
- The component/directive class is named without the type (e.g.
Widget), as configured. - The NgModule
declarations(andexportswith--export) import the name with the type
(e.g.WidgetComponent), so the build fails.
This is the configuration that ng new --no-standalone --file-name-style-guide=2016 writes to angular.json:
"@schematics/angular:component": { "type": "component", "addTypeToClassName": false },
"@schematics/angular:directive": { "type": "directive", "addTypeToClassName": false }
The component and directive schematics compute the class name with addTypeToClassName
(packages/schematics/angular/component/index.ts, packages/schematics/angular/directive/index.ts), but the shared
addDeclarationToNgModule helper always appends the type:
// packages/schematics/angular/utility/add-declaration-to-ng-module.ts
const classifiedName =
strings.classify(options.name) + (options.type ? strings.classify(options.type) : '');
Minimal Reproduction
ng new ws --no-standalone --file-name-style-guide=2016 --defaults
cd ws
ng generate component widget --module app
ng build
Generated src/app/widget/widget.component.ts:
export class Widget {}
Updated src/app/app.module.ts:
import { WidgetComponent } from './widget/widget.component';
@NgModule({
declarations: [App, WidgetComponent],
The same happens for ng generate directive ... --module.
Exception or Error
✘ [ERROR] TS2305: Module '"./widget/widget.component"' has no exported member 'WidgetComponent'. [plugin angular-compiler]
✘ [ERROR] NG1010: Value at position 1 in the NgModule.declarations of AppModule is not a reference
Your Environment
Angular CLI : 22.2.3
Node.js : 24.15.0
Package Manager : npm 11.12.1
Operating System : darwin arm64
@schematics/angular : 22.2.3
Anything else relevant?
- Introduced together with
addTypeToClassNamein e417c89f9e (#31053, v21.0.0). The same code is on21.2.xand22.2.x. - Suggested fix: let
addDeclarationToNgModuleuse the class name computed by the calling schematic (or honour
addTypeToClassName), so the declaration matches the generated class. Happy to send a PR with specs for the
component and directive schematics.
- 主要语言
- TypeScript
- 星标
- 27k
- 派生
- 11.8k
- 平均合并
- 1 天 6 小时
- 30 天内合并 PR
- 216
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
angular/angular-cli 的其他 Issue
-
area: @angular/build gemini-triaged
难度 2/5 1-3 小时 新手友好度 74/100
angular/angular-cli#33955 ·
维护者通常 1 天内回复
-
area: @angular/cli gemini-triaged
难度 2/5 1-3 小时 新手友好度 72/100
angular/angular-cli#33055 · 1 条评论 · 3 个 reaction ·
维护者通常 1 天内回复
-
@angular/cli 21.2.x and 20.3.x depend on @modelcontextprotocol/[email protected] which is vulnerable to CVE-2026-111767, CVE-2026-111768, CVE-2026-111769 and CVE-2026-111771可能已有人在做 @florimond-collette 于 1 天前认领。 未关闭area: @angular/cli
难度 2/5 1-3 小时 新手友好度 35/100
angular/angular-cli#34455 ·
维护者通常 1 天内回复
-
@angular/build 22.2.x, 21.2.x and 20.3.x depend on [email protected] / [email protected] which are vulnerable to CVE-2026-112943可能已有人在做 @florimond-collette 于 1 天前认领。 未关闭area: @angular/build
难度 3/5 1-2 天 新手友好度 25/100
angular/angular-cli#34452 ·
维护者通常 1 天内回复
-
`ng generate app-shell` and `ng generate module --route` produce broken imports when a component `type` is configured可能已有人在做 @MeAkib 于 1 天前认领。 未关闭area: @schematics/angular
难度 3/5 1-2 天 新手友好度 35/100
angular/angular-cli#34449 ·
维护者通常 1 天内回复
查看 angular/angular-cli 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
endora-commerce/endora-commerce#397 ·
维护者通常 1 天内回复
-
level/task
难度 2/5 1-3 小时 新手友好度 68/100
wazuh/wazuh-dashboard-plugins#9472 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 63/100
维护者通常 1 天内回复
-
ready-for-agent
难度 2/5 1-3 小时 新手友好度 68/100
xavxyz/bei-shan-lin-shui#57 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
opennextjs/opennextjs-cloudflare#1461 ·
维护者通常 1 天内回复