You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Which @angular/* package(s) are the source of the bug?
compiler-cli
Is this a regression?
No
Description
I'm trying createComponent function.
The Docs says it also works with NgModule components, but I got a compile error.
reproduction steps:
ng new dynamic-create-ng-module-component-bug --skip-tests --routing=false --ssr=false
cd dynamic-create-ng-module-component-bug\src\app
create Test module, Test1 and Test2 components. ng g m test & ng g c test/test1 --standalone=false -m=test & ng g c test/test2 --standalone=false -m=test
use <app-test1 /> in test2.component.html
<p>test2 works!</p>
<app-test1 />
create Test2Component in App by createComponent function
export class AppComponent {
title = 'dynamic-create-ng-module-component-bug';
constructor() {
createComponent(Test2Component, {
environmentInjector: inject(EnvironmentInjector),
});
}
}
run ng serve. now will get a error: [ERROR] NG8001: 'app-test1' is not a known element
import TestModule in app.component.ts but without add it into App @Component.imports
import { Component, EnvironmentInjector, createComponent, inject } from '@angular/core';
import { Test2Component } from './test/test2/test2.component';
import { TestModule } from './test/test.module'; // import TestModule
@Component({
selector: 'app-root',
standalone: true,
templateUrl: './app.component.html',
styleUrl: './app.component.css',
imports: [] // no need put TestModule into imports array
})
export class AppComponent {
title = 'dynamic-create-ng-module-component-bug';
constructor() {
createComponent(Test2Component, {
environmentInjector: inject(EnvironmentInjector),
});
}
}
run ng serve again, now it works!
I am not 100% sure it is a bug, but it looks not so logical and the Docs said
Note: the example uses standalone components, but the function can also be used for non-standalone components (declared in an NgModule) as well.
so I made this bug report.
Please provide a link to a minimal reproduction of the bug
X [ERROR] NG8001: 'app-test1' is not a known element:
1. If 'app-test1' is an Angular component, then verify that it is part of this module.
2. If 'app-test1' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. [plugin angular-compiler]
Please provide the environment you discovered this bug in (run ng version)
Thank you for your quick reply. I know how to fix the issue, as demonstrated in step 7.
but I wonder why Angular docs does not specifically highlight this.
And it is not so logical to import TestModule but never used it can make the compiler work properly.
it made sense if we need to import TestModule and put into the App @Component.imports array.
can you explain to me in more detail or give me a direction to learn about this part?
the Angular learning curve is steep🙁
Which @angular/* package(s) are the source of the bug?
compiler-cli
Is this a regression?
No
Description
I'm trying createComponent function.
The Docs says it also works with NgModule components, but I got a compile error.
reproduction steps:
ng new dynamic-create-ng-module-component-bug --skip-tests --routing=false --ssr=falsecd dynamic-create-ng-module-component-bug\src\appng g m test & ng g c test/test1 --standalone=false -m=test & ng g c test/test2 --standalone=false -m=testI am not 100% sure it is a bug, but it looks not so logical and the Docs said
so I made this bug report.
Please provide a link to a minimal reproduction of the bug
https://github.com/keatkeat87/dynamic-create-ng-module-component-bug
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version)Anything else?
No response
The text was updated successfully, but these errors were encountered: