[英]What's the best way to get a ComponentFactory in Angular 4 Dart?
There is documentation in the AngularDart Github repo for Component loading ( https://github.com/dart-lang/angular/blob/master/doc/component_loading.md ) that recommends importing a component's template file (which is generated) and then accessing a property on that template where you just take the component's name and append NgFactory to it. 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;
// ^^^^^^^^^
}
But my IDE (IntelliJ) can't see foo.template.dart as it has not been generated yet and gives me warnings and then it also knows nothing about the FooComponentNgFactory
. 但我的IDE(IntelliJ)无法看到foo.template.dart,因为它还没有生成并给我警告,然后它也对FooComponentNgFactory
。 With Dart being dynamic, this all works out at runtime, but it leaves me with warnings / errors in my IDE which I don't want to be in the habit of having to ignore and the whole thing feels like a code smell to me. 由于Dart是动态的,这一切都在运行时运行,但它在我的IDE中留下了警告/错误,我不想养成不得不忽视的习惯,整个事情对我来说就像代码味道。
I was also able to get a factory by using a ComponentResolver, which did not involve needing to use the generated template files, or NgFactory
. 我还可以通过使用ComponentResolver来获得工厂,这不需要使用生成的模板文件或NgFactory
。 The only downside that I can see is that it is an async
method, which in my case is not a problem. 我能看到的唯一缺点是它是一个async
方法,在我的情况下不是问题。
@Component(selector: 'some-component', template: '')
class SomeComponent {
SomeComponent(this._resolver);
ComponentResolver _resolver;
Future doSomething() async {
ComponentFactory factory = await _resolver.resolveComponent(FooComponent);
}
}
In looking at ComponentResolver's documentation I don't see any reason not to use it ( https://webdev.dartlang.org/api/angular/angular/ComponentResolver-class ). 在查看ComponentResolver的文档时,我认为没有任何理由不使用它( https://webdev.dartlang.org/api/angular/angular/ComponentResolver-class )。
Why is this not the accepted way to get a ComponentFactory? 为什么这不是获得ComponentFactory的可接受方式? Am I doing something wrong with the NgFactory
or is there something that I need to be careful with using ComponentResolver
that I'm missing? 我是否在使用NgFactory
做错了什么,或者我需要注意使用我缺少的ComponentResolver
?
There are two reasons we don't want to use resolver: 我们不想使用解析器有两个原因:
Tree shaking is the big reason though. 树摇是一个重要原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.