这个问题通常是由于惰性加载模块导致的。当你导航到一个含有app-image-croper的懒加载模块中时,Angular将无法识别该元素。
解决方法是在你的懒加载的模块中导入app-image-croper对应的模块,如下所示:
// app/image-croper/image-croper.module.ts
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ImageCroperComponent } from './image-croper.component';
@NgModule({ declarations: [ImageCroperComponent], imports: [CommonModule], exports: [ImageCroperComponent], }) export class ImageCroperModule {}
// app/image-croper/image-croper.component.ts
import { Component, OnInit } from '@angular/core';
@Component({ selector: 'app-image-croper', templateUrl: './image-croper.component.html', styleUrls: ['./image-croper.component.scss'], }) export class ImageCroperComponent implements OnInit { constructor() {}
ngOnInit(): void {} }
// app/lazy-loaded-module/lazy-loaded.module.ts
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterModule } from '@angular/router'; import { ImageCroperModule } from '../image-croper/image-croper.module'; import { LazyLoadedComponent } from './lazy-loaded.component';
@NgModule({ declarations: [LazyLoadedComponent], imports: [CommonModule, RouterModule.forChild([{ path: '', component: LazyLoadedComponent }]), ImageCroperModule], exports: [LazyLoadedComponent], }) export class LazyLoadedModule {}
这样,在懒加载模块中的组件就可以使用app-image-croper元素了。