【问题标题】:Angular - Add component B inside component AAngular - 在组件 A 中添加组件 B
【发布时间】:2017-08-22 19:12:00
【问题描述】:

我的项目中有以下结构,

src
- component-a
-- component-a.component.ts
-- component-a.html
-- component-a.scss
-- component-a.component.spec.ts

- component-b
-- component-b.component.ts
-- component-b.html
-- component-b.scss
-- component-b.component.spec.ts

- app.component.ts
- app.module.ts
- app.html

我在 app.component.ts 中使用 component-a,因此我已将其包含在 app.module.ts 的声明中。

declarations: [
        App, ComponentA,
    ],

在 app.html 中:<component-a></component-a>

现在我想在 component-a 中添加 component-b。 因此,无论我尝试了什么,当我在 component-a.html 中使用 <component-b></component-b> 时,我都会收到此错误:

Unhandled Promise rejection: Template parse errors:
'component-b' is not a known element:
1. If 'component-b' is an Angular component, then verify that it is part of this module.
2. If 'component-b' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
      [ERROR ->]<component-b></component-b>
"): ng:///AppModule/ComponentA.html@7:6 ; Zone: <root> ; Task: Promise.then ; Value: 

component-a.html中如何使用component-b?

  • 我尝试在component-a.component.ts中导入component-b
  • 我尝试在 app.module.ts 中导入和添加组件-b 声明。

【问题讨论】:

  • 您必须为组件 A (ng g module 组件-a) 创建单独的模块,将组件-a 和组件-b 移到那里,然后将该模块导入主模块 (app.module)。
  • 试试这个fix,这可能会解决你的问题
  • 试试这个fix,这可能会解决你的问题
  • 您可以使用entryComponents
  • 你为什么不把ComponentB 添加到declarationsapp.module 就像你对ComponentA 所做的那样?

标签: angular angular-cli


【解决方案1】:
  1. 为这两个组件创建一个模块。
  2. component B导入component A的模块imports
  3. 完成。

示例(A 模块)

import {NgModule} from '@angular/core';

import {BModule} from '@components/b/b.module';

@NgModule({
  imports: [
    BModule
  ],
  exports: [
    AComponent
  ],
  declarations: [
    AComponent
  ]
})

export class AModule {}

示例(B 模块):

import {NgModule} from '@angular/core';

@NgModule({
  exports: [
    BComponent
  ],
  declarations: [
    BComponent
  ]
})

export class BModule {}

【讨论】:

  • 嗨。我按照这些步骤操作,错误仍然显示。你知道为什么会这样吗? :(
  • 在 b.module 中我有什么需要做的吗?
  • 您必须导出组件并声明它。就像上面的例子一样。我会把它添加到答案中。
  • 还值得一提的是,两个模块都应该导入一个共享模块,其中声明了公共模块、管道和指令。您可以在 Angular 的官方样式指南中阅读有关共享模块的信息。
【解决方案2】:

正如我们在评论部分所讨论的。您可以在 app.module 中提供 ComponentB 以将其导入整个应用程序。

import {NgModule} from '@angular/core';

@NgModule({
  ...
  declarations: [
    AComponent, BComponent
  ]
})    
export class AppModule {}

http://plnkr.co/edit/H929znCRMcqlwuI4ySvp?p=preview

【讨论】:

  • 它可以,但很可能不是,除非它是一个爱好项目。我只是不喜欢示例向您展示 a 的方式,但不一定是您应该或将在实际示例中使用的方式。然后您更有可能始终使用该解决方案,因为“这就是我一开始就学会这样做的方式”。你的答案有效,但我的更完整。所以我想我们的答案是相辅相成的。
  • @Chrillewoodz 我完全支持你的理想主义思想,但我不同意给出“专业”答案的想法。我确定您没有从 SO 答案中学习延迟加载,至少是它的核心概念。初学者很难在不了解核心概念的情况下从模块/延迟加载概念开始。如果你提出这些类型的上下文,那么对于初学者来说它会变得更加复杂。对于初学者来说,您的答案有点复杂,正如 OP 在 cmets 中提到的那样,它无法处理该任务。这可能有很多原因,因为我们只知道架构的一部分。
  • @Chrillewoodz 在我看来,复杂的答案可能会阻止初学者学习角度,因为事情不会很快发生。但如果他们在 html 中看到“Hello World”,我相信他们会更深入地研究文档。
  • 是的,我同意。在发布答案时,我通常不会考虑此人的个人技能,也许我应该听取您的建议并开始这样做。
  • @Chrillewoodz 我会记住给出更多现实生活中合适的答案:-)
猜你喜欢
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 2018-01-16
  • 2019-05-13
  • 1970-01-01
  • 2018-03-29
相关资源
最近更新 更多