angular dart:是否可以注入组件?

angular dart: is it possible to inject a component?

飞镖 1.24.3,Angular4.0.0

出于可重用的原因,我将我的应用程序分成了很多包。 假设我正在使用包 C,它依赖于包 B,而包 B 又依赖于包 A。A 也在其他包中使用,而 B 可以独立使用。 最后,C 向 B 添加更多信息,它在某种程度上是自定义的。

现在,在 A 包中,我有一个显示一些信息的模态 window,但在 B 包中,我应该添加一个组件,在 C 包中还要添加一个组件。 由于此模态 window 用于我所有的组件表单,我无法在子包中自定义它,因为这意味着继承所有父表单并自定义它们。 我想使用的是类似于提供程序注入的东西(例如我想在包 C 中的应用程序组件中声明的内容):

 ModalWindowComponent,
      const Provider(pack_b.ModalWindowComponent, useClass: ModalWindowComponent),
      const Provider(pack_a.ModalWindowComponent, useClass: ModalWindowComponent)

谷歌搜索我看到了一些我可以在我的案例中使用的东西,ComponentResolver class。但是我不知道它是否是 Angular 5 的新东西,或者也可以在 angular 4 中使用。在其他一些地方,它似乎也会被弃用,所以我有点使困惑。 此外,我不需要在 DOM 中动态添加组件,而是用子包中的组件(继承基础组件)动态替换组件。

有办法吗?有没有我可以看一下的例子?

您可以使用 ComponentLoader 这需要传递 ComponentFactory 和 DOM 中的一个位置。方法文档中有很好的示例。

您可以通过导入具有 @Component 注释的 .template.dart 文件并在组件名称后附加 NgFactory 来获得组件的 ComponentFactory,例如:

文件foo.dart:

@Component(selector: 'foo')
class FooComponent {}

文件bar.dart:

import 'foo.template.dart';

@Component(selector: 'bar', template: '<div #placeholder></div>')
class BarComponent {
  final ComponentLoader _loader;

  @ViewChild('placeholder', read: ViewContainerRef)
  ViewContainerRef theDiv;

  BarComponent(this._loader);

  void load() {
    _loader.loadNextToLocation(FooComponentNgFactory, theDiv);
  }
}