在 Angular 4 Dart 中获取 ComponentFactory 的最佳方法是什么?

What's the best way to get a ComponentFactory in Angular 4 Dart?

AngularDart Github 组件加载 (https://github.com/dart-lang/angular/blob/master/doc/component_loading.md) 存储库中的文档建议导入组件的模板文件(已生成),然后访问 属性模板,您只需使用组件的名称并将 NgFactory 附加到它。

import 'foo.template.dart' as ng;

void doSomething() {
  ng.FooComponentNgFactory;
  //             ^^^^^^^^^
}

但是我的 IDE (IntelliJ) 看不到 foo.template.dart,因为它还没有生成并给我警告,然后它对 FooComponentNgFactory 也一无所知。由于 Dart 是动态的,这一切都在运行时运行,但它在我的 IDE 中留下警告/错误,我不想养成不得不忽略的习惯,整个事情感觉就像代码我闻起来。

我还能够通过使用 ComponentResolver 获得一个工厂,这不需要使用生成的模板文件,或者 NgFactory。我能看到的唯一缺点是它是一种 async 方法,在我的情况下这不是问题。

@Component(selector: 'some-component', template: '')
class SomeComponent {
  SomeComponent(this._resolver);

  ComponentResolver _resolver;

  Future doSomething() async {
    ComponentFactory factory = await _resolver.resolveComponent(FooComponent);
  }
}

在查看 ComponentResolver 的文档时,我没有看到任何不使用它的理由 (https://webdev.dartlang.org/api/angular/angular/ComponentResolver-class)。

为什么这不是获得 ComponentFactory 的公认方式?我是在使用 NgFactory 做错了什么,还是在使用 ComponentResolver 时需要注意我遗漏的东西?

我们不想使用解析器有两个原因:

  • 它需要组件类型和工厂之间的映射。 有了这张地图,Dart2JS 不知道这段代码是否被使用,并且它 不能摇树。目前任何导入的组件都在我们这边 可执行文件,即使我们不使用它们。
  • 我们可以通过不需要异步操作来让它更快一点。

摇树是主要原因。