skip to Main Content

I am not able to use a header component’s selector tag in another component, but works fine in app.component.html alone

header.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'cvy-header',
  templateUrl: './header.component.html',
  styleUrl: './header.component.scss',
})
export class HeaderComponent {

}

app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule, provideClientHydration } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';


import { LoginComponent } from './pages/login/login.component';

import { ElectricalsComponent } from './pages/pages/electricals.component';
import { sharedComponent } from './pages/shared/components/shared.component';

@NgModule({
  declarations: [
    AppComponent,
    ...sharedComponent,
    LoginComponent,
    ElectricalsComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
  ],
  providers: [
    provideClientHydration()
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

shared.component.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from './header/header.component';
import { SideBarComponent } from './side-bar/side-bar.component';

export const sharedComponent:any[] = [
  HeaderComponent,
  SideBarComponent
] 

export * from './header/header.component';
export * from './side-bar/side-bar.component';

electricals.component.html

<cvy-header></cvy-header>

It is throwing
‘cvy-header’ is not a known element:

  1. If ‘cvy-header’ is an Angular component, then verify that it is part of this module.

But in app.component.html,

<cvy-header></cvy-header>

It works,
I have tried all means but am not able to solve this and am getting

‘cvy-header’ is not a known element:

  1. If ‘cvy-header’ is an Angular component, then verify that it is part of this module.
  2. If ‘cvy-header’ is a Web Component then add ‘CUSTOM_ELEMENTS_SCHEMA’ to the ‘@NgModule.schemas’ of this component to suppress this message.

2

Answers


  1. I suspect the order of the components in declarations.

    Try to change the order of your declaration to make sure all components are available.

    Before:

      declarations: [
        AppComponent,
        ...sharedComponent,
        LoginComponent,
        ElectricalsComponent,
      ]
    

    After:

      declarations: [
        AppComponent,
        LoginComponent,
        ElectricalsComponent, <——- 1
        ...sharedComponent, <——- 2
      ],
    

    The Electricals component is injected before sharedComponent.

    Login or Signup to reply.
  2. @Yasik raam Ensure that ElectricalsComponent is part of AppModule or else any other module, If it is not part of the AppModule you have to export HeaderComponent from AppModule and import it into the feature module.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search