Angular 9 - 不是一个已知元素。

4
它在 Angular 7 中工作,现在更新后会抛出未知元素错误。这种情况发生在共享模块中。
可复现的代码:- https://stackblitz.com/edit/angular-ivy-pb3eea 我有一个父组件 'Parent',在其中使用子组件 'Child',并在父组件和子组件中都使用共享组件 'Shared'。
因此,我已经在父组件和子组件的两个模块中都导入了共享组件。(因为使用了懒加载模块)在父组件中它可以工作,但在子组件中却不行。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TestChildRoutingModule} from './test-child.routing.module';
import { SharedModule } from '../shared/shared.module';

@NgModule({
    declarations: [
    ],
    imports: [
        CommonModule,
        SharedModule,
        TestChildRoutingModule
    ],
    exports: [
    ]
})
export class TestChildModule { }

1个回答

5

你在 TestChildModule 中缺少对 TestChildComponent 的声明

@NgModule({
    declarations: [
      TestChildComponent
    ],
    imports: [
        CommonModule,
        SharedModule,
        TestChildRoutingModule
    ],
    exports: [
      TestChildComponent
    ]
})
export class TestChildModule { }

这是已修复的STACKBLITZ


2
我正要发同样的帖子 :) +1 - Pardeep Jain
1
那是一个非常愚蠢的错误。谢谢Sajeethran和@pradeep。 - Luckyy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接