Angular 2 'component' is not a known element

后端 未结 15 1970
傲寒
傲寒 2020-11-30 17:58

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: Temp

相关标签:
15条回答
  • 2020-11-30 18:55

    I was facing the same issue. In my case I have forgotten to declare Parent component in the app.module.ts

    As a example if you are using <app-datapicker> selector in ToDayComponent template, you should declare both ToDayComponent and DatepickerComponent in the app.module.ts

    0 讨论(0)
  • 2020-11-30 18:56

    I am beginning Angular and in my case, the issue was that I hadn't saved the file after adding the 'import' statement.

    0 讨论(0)
  • 2020-11-30 18:57

    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();
      });
    }
    
    0 讨论(0)
  • 2020-11-30 18:58

    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.

    0 讨论(0)
  • 2020-11-30 18:58

    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

    0 讨论(0)
  • 2020-11-30 18:59

    This question may seem old and odd, but when I was trying to load a module(lazy loading) and getting the same error, I realized I was missing an exports clause for the component that shipped as a part of a larger module.

    This Angular.io Link explains why: Components/Services inside a module, remains private(or protected) by default. To make them public, you have to export them.

    Expanding on @Robin Djikof's answer with @live-love code sample, this is what was technically missing in my case(Angular 8):

    @NgModule({
      declarations: [
        SomeOtherComponent,
        ProductListComponent
      ],
      imports: [
        DependantModule
      ],
      exports: [ProductListComponent] 
      //<- This line makes ProductListComponent available outside the module, 
      //while keeping SomeOtherComponent private to the module
    })
    export class SomeLargeModule { }
    
    0 讨论(0)
提交回复
热议问题