问题
I'm trying to use a component I created inside the AppModule in other modules. I get the following error though:
"Uncaught (in promise): Error: Template parse errors:
'contacts-box' is not a known element:
- If 'contacts-box' is an Angular component, then verify that it is part of this module.
- If 'contacts-box' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
My project structure is quite simple:
I keep my pages in pages directory, where each page is kept in different module (e.g. customers-module) and each module has multiple components (like customers-list-component, customers-add-component and so on). I want to use my ContactBoxComponent inside those components (so inside customers-add-component for example).
As you can see I created the contacts-box component inside the widgets directory so it's basically inside the AppModule. I added the ContactBoxComponent import to app.module.ts and put it in declarations list of AppModule. It didin't work so I googled my problem and added ContactBoxComponent to export list as well. Didn't help. I also tried putting ContactBoxComponent in CustomersAddComponent and then in another one (from different module) but I got an error saying there are multiple declarations.
What am I missing?
回答1:
These are the 5 steps I perform when I got such an error.
- Are you sure the name is correct? (also check the selector defined in the component)
- Declare the component in a module?
- If it is in another module, export the component?
- If it is in another module, import that module?
- Restart the cli?
I also tried putting ContactBoxComponent in CustomersAddComponent and then in another one (from different module) but I got an error saying there are multiple declarations.
You can't declare a component twice. You should declare and export your component in a new separate module. Next you should import this new module in every module you want to use your component.
It is hard to tell when you should create new module and when you shouldn't. I usually create a new module for every component I reuse. When I have some components that I use almost everywhere I put them in a single module. When I have a component that I don't reuse I won't create a separate module until I need it somewhere else.
Though it might be tempting to put all you components in a single module, this is bad for the performance. While developing, a module has to recompile every time changes are made. The bigger the module (more components) the more time it takes. Making a small change to big module takes more time than making a small change in a small module.
回答2:
I had a similar issue. It turned out that ng generate component
(using CLI version 7.1.4) adds a declaration for the child component to the AppModule, but not to the TestBed module that emulates it.
The "Tour of Heroes" sample app contains a HeroesComponent
with selector app-heroes
. The app ran fine when served, but ng test
produced this error message: 'app-heroes' is not a known element
. Adding the HeroesComponent
manually to the declarations in configureTestingModule
(in app.component.spec.ts
) eliminates this error.
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
HeroesComponent
],
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
}
回答3:
I just had the exact same issue. Before trying some of the solutions posted here, you might want to check if the component really doesn't work. For me, the error was shown in my IDE (WebStorm), but it turned out that the code worked perfectly when i ran it in the browser.
After I shut down the terminal (that was running ng serve) and restarted my IDE, the message stopped showing up.
回答4:
I have the same issue width php storm version 2017.3. This fix it for me: intellij support forum
It was an error width @angular language service: https://www.npmjs.com/package/@angular/language-service
回答5:
I got the same issue, and it was happening because of different feature module included this component by mistake. When removed it from the other feature, it worked!
回答6:
The problem in my case was missing component declaration in the module, but even after adding the declaration the error persisted. I had stop the server and rebuild the entire project in VS Code for the error to go away.
回答7:
In my case, my app had multiple layers of modules, so the module I was trying to import had to be added into the module parent that actually used it pages.module.ts
, instead of app.module.ts
.
来源:https://stackoverflow.com/questions/44429996/angular-2-component-is-not-a-known-element