在AngularDart Github仓库中有组件加载(https://github.com/dart-lang/angular/blob/master/doc/component_loading.md)的文档,它建议导入组件's template file (which is generated) and then accessing a property on that template where you just take the component'的名称并将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 's documentation I don'时,请查看不使用它的任何原因(https://webdev.dartlang.org/api/angular/angular/ComponentResolver-class) .
为什么这不是获得ComponentFactory的可接受方式?我在 NgFactory
做错了什么,或者我需要小心使用 ComponentResolver
我错过了什么?
1 回答
我们不想使用解析器有两个原因:
它需要组件类型和工厂之间的映射 . 有了这张 Map ,Dart2JS就不会被震动了 . 目前,即使我们不使用它们,任何导入的组件都在我们的最终可执行文件中 .
我们可以通过不需要异步操作使其快一点 .
树摇是一个重要原因 .