Home > Net >  Changing the component selector failed compilation
Changing the component selector failed compilation

Time:01-14

It is Angular CLI 13.1.3

I have a component inside a module. Module name is auth and component name is login. By default, login component had selector name "app-login" which I changed into "login"

and placed the new name in app.component.html

Code in login component inside auth module

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

@Component({
  selector: 'login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }
}

It showed following errors.

src/app/app.component.html:1:1 - error NG8001: 'login' is not a known element:

  1. If 'login' is an Angular component, then verify that it is part of this module.
  2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.

enter image description here

Do I need to make further changes for login selector change?

Code In Auth Module

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';

@NgModule({
  declarations: [
    LoginComponent
  ],
  imports: [
    CommonModule
  ]
})
export class AuthModule { }

Code in App Module

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

import { AppComponent } from './app.component';
import { AuthModule } from './Auth/Auth.module';
import { HeaderComponent } from './header/header.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent
  ],
  imports: [
    AuthModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

CodePudding user response:

For AppComponent to be able to call LoginComponent, your AuthModule has to export LoginComponent:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';

@NgModule({
  declarations: [
    LoginComponent
  ],
  imports: [
    CommonModule
  ],
  exports: [
    LoginComponent // <=== here
  ]
})
export class AuthModule { }
  •  Tags:  
  • Related