模块和角度组件不是已知元素错误

问题描述 投票:0回答:1

我有一个这样的模块:

// app.module.ts
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
...
],
  imports: [
    BrowserModule,
    SharedModule,
...
]
})
export class AppModule {}

还有一个像这样的模块:

// shared.module.ts
@NgModule({
  declarations: [
    myComponent,
    myOtherComponent,
...
],
  imports: [
    OtherModule,
...
],
  exports: [
    myComponent,
    myOtherComponent,
...
})
export class SharedModule {}

在 myComponent 模板中我有这样的东西:

<div>
<my-other-component></my-other-component>
</div>

应用程序运行完美,但在 VSC 中我看到标签 my-other-component 就像一个错误, 说的是 “我的其他组件”不是已知元素:

  1. 如果“my-other-component”是 Angular 组件,则验证它是否是该模块的一部分。
  2. 如果 'my-other-component' 是 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到该组件的 '@NgModule.schemas' 以抑制此消息。ngtsc(-998001) `

通常,当我收到此错误时,我只需将 MyOtherComponent 添加到 AppModule 或 SharedModule 即可修复......但这次不是。 为什么?

非常感谢!

我也尝试将 MyOtherComponent 添加到 AppComponent,但在浏览器上我收到另一个错误:

compiler.js:2175 Uncaught Error: Type MyOtherComponent is part of the declarations of 2 modules: SharedModule and AppModule!

angular module angular-module
1个回答
0
投票

您已从

myComponent
导出
myOtherComponent
SharedModule
,并在同一文件中再次导入
SharedModule
。从sharedmodule.ts
的导入中删除
SharedModule

© www.soinside.com 2019 - 2024. All rights reserved.