【问题标题】:Angular: Component declared in app.module.ts not found in other componentsAngular:在 app.module.ts 中声明的组件在其他组件中找不到
【发布时间】:2021-04-05 21:34:56
【问题描述】:

我有一个自定义模式,我想在其他几个组件中重用。但作为一个组件,它没有找到。

获取:

NG0304: 'app-addtoplanner' is not a known element:
1. If 'app-addtoplanner' is an Angular component, then verify that it is part of this module.
2. If 'app-addtoplanner' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

我在基础/根 app.module 中以两种方式在 app.module 中注册了组件:
1. 与 "declarations: [ ]" 和
中的组件一样 2.我也尝试过为自己制作一个模块并在“imports:[]”中注册。

无论如何它都不可用。

除了模板中的选择器之外,我没有在子组件(组件或子模块)中包含对它的引用。

我可以在添加到该组件自己的模块时在单个子组件上使用它,所以我知道它正在工作。但不能重用(这很烦人,因为它是 Angular 的基本租户,哈哈)

有什么建议吗?

ps。我的模块从第 2 点开始的内容如下:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { AddToPlannerComponent } from './add-to-planner/add-to-planner.component';

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
  AddToPlannerComponent
  ],
 exports:
  [
    AddToPlannerComponent,
    CommonModule
  ]
})
export class AddToPlannerModule { }

在我的 app.module 中导入

import {AddToPlannerModule} from './views/planner/add-to-planner.module'

@NgModule({
   imports: [
      AddToPlannerModule,
...

【问题讨论】:

  • 你是导入另一个模块还是同一个模块?
  • 从上面适当地埋在目录树中的模块导出到根app.module中

标签: angular modal-dialog angular-components


【解决方案1】:

如果组件在另一个模块中声明,您也必须导出它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2021-04-28
    • 2022-01-02
    • 1970-01-01
    • 2021-08-24
    • 2018-07-05
    • 2023-03-06
    相关资源
    最近更新 更多