If '' is an Angular component, then verify that it is part of this module

前端 未结 16 3609
慢半拍i
慢半拍i 2020-12-07 14:09

I am new in Angular2. I have tried to create a component but showing an error.

This is the app.component.ts file.

import { Component } f         


        
相关标签:
16条回答
  • 2020-12-07 14:58

    In my case I was writing unit tests for a component that uses sub-components, and I was writing tests to make sure those subcomponents were in the template:

    it('should include the interview selection subview', () => {
        expect(fixture.debugElement.query(By.css('app-interview')))
        .toBeTruthy()  
      }); 
    

    I didn't get an error, but a warning:

    WARN: ''app-interview' is not a known element:

    1. If 'app-interview' is an Angular component, then verify that it is part of this module. WARN: ''app-interview' is not a known element:
    2. If 'app-interview' is an Angular component, then verify that it is part of this module.
    3. If 'app-interview' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'

    Also, the subcomponent did not show inside the browser during testing.

    I used ng g c newcomponent to generate all the components, so they were already declared in the appmodule, but not the test module that for the component I was spec'ing.

    beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ EditorComponent,
            InterviewComponent]
        })
        .compileComponents();
      }));
    
    0 讨论(0)
  • 2020-12-07 15:02

    In my case I had a component in a Shared module.

    The component was loading and worked well but typescript was highlighting the html tag with red line and showed this error message.

    Inside this component, I noticed I didn't import a rxjs operator.

    import {map} from 'rxjs/operators';
    

    When I added this import the error message disappeared.

    Check all imports inside the component.

    Hope it helps someone.

    0 讨论(0)
  • 2020-12-07 15:06

    Hope you are having app.module.ts. In your app.module.ts add below line-

     exports: [myComponentComponent],
    

    Like this:

    import { NgModule, Renderer } from '@angular/core';
    import { HeaderComponent } from './headerComponent/header.component';
    import { HeaderMainComponent } from './component';
    import { RouterModule } from '@angular/router';
    
    @NgModule({
        declarations: [
            HeaderMainComponent,
            HeaderComponent
        ],
        imports: [
            RouterModule,
        ],
        providers: [],
        bootstrap: [HeaderMainComponent],
        exports: [HeaderComponent],
    })
    export class HeaderModule { }
    
    0 讨论(0)
  • 2020-12-07 15:07

    Your MyComponentComponent should be in MyComponentModule.

    And in MyComponentModule, you should place the MyComponentComponent inside the "exports".

    Something like this, see code below.

    @NgModule({
       imports: [],
       exports: [MyComponentComponent],
       declarations: [MyComponentComponent],
       providers: [],
    })
    
    export class MyComponentModule {
    }
    

    and place the MyComponentModule in the imports in app.module.ts like this (see code below).

    import { MyComponentModule } from 'your/file/path';
    
    @NgModule({
       imports: [MyComponentModule]
       declarations: [AppComponent],
       providers: [],
       bootstrap: [AppComponent]
    })
    
    export class AppModule {}
    

    After doing so, the selector of your component can now be recognized by the app.

    You can learn more about it here: https://angular-2-training-book.rangle.io/handout/modules/feature-modules.html

    Cheers!

    0 讨论(0)
提交回复
热议问题