Angular 12中父模块组件在ng-template渲染组件内不可见问题
解决Angular 12嵌套布局里uni组件识别失败的问题
问题核心是Angular的模块作用域规则:组件只能在导入了对应组件/模块的NgModule范围内使用。你之前的lista1livello-layout所在模块已经导入了uni组件的依赖模块,所以能正常渲染;但嵌套的lista1livello-search所在模块没加这个导入,编译自然认不出这些uni-*标签。
按下面步骤修复:
定位
lista1livello-search所属的模块
打开lista1livello-search.component.ts,找到它对应的NgModule文件——要么是单独的lista1livello-search.module.ts,要么是在lista1livello-layout.module.ts里直接声明的。在对应模块中导入uni组件的依赖模块
假设你的uni组件都封装在UniComponentsModule里,在目标模块的imports数组中添加该模块:import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Lista1LivelloSearchComponent } from './lista1livello-search.component'; // 替换成你项目中uni组件模块的实际路径 import { UniComponentsModule } from '../../path/to/uni-components.module'; @NgModule({ declarations: [Lista1LivelloSearchComponent], imports: [ CommonModule, UniComponentsModule // 关键:添加这一行导入 ], exports: [Lista1LivelloSearchComponent] // 如果该组件需要被其他模块引用,必须导出 }) export class Lista1LivelloSearchModule { }如果
lista1livello-search是直接在lista1livello-layout的模块里声明的,直接在lista1livello-layout.module.ts的imports中添加UniComponentsModule即可。检查uni模块的导出配置
打开uni组件的模块文件(比如UniComponentsModule),确保exports数组里包含了你需要用的所有组件:@NgModule({ declarations: [ UniBaseLoaderLayoutComponent, UniLoadingComponent, UniCardsLoaderComponent ], exports: [ UniBaseLoaderLayoutComponent, UniLoadingComponent, UniCardsLoaderComponent // 每个要在外层使用的组件都得导出 ] }) export class UniComponentsModule { }清理缓存重新编译
有时候Angular缓存会导致模块识别异常,执行ng clean后再重新运行ng serve即可。
内容的提问来源于stack exchange,提问作者ilbassa
相关产品推荐
相关产品推荐

