更改组件选择器编译失败

Changing the component selector failed compilation

是Angular CLI 13.1.3

我在模块中有一个组件。模块名称是 auth,组件名称是 login。默认情况下,登录组件的选择器名称为“app-login”,我将其更改为“login”

并将新名称放入 app.component.html

auth 模块中登录组件中的代码

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 {
  }
}

显示以下错误。

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.

我是否需要对登录选择器进行进一步更改?

授权模块中的代码

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

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

应用程序模块中的代码

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 { }

要让 AppComponent 能够调用 LoginComponent,您的 AuthModule 必须导出 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 { }