简体   繁体   English

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

[英]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: 我们不想使用解析器有两个原因:

  • It needs a mapping between the Component Type and the Factory. 它需要组件类型和工厂之间的映射。 With this map Dart2JS doesn't know if this code is being used and it can't be tree shaken. 使用此映射,Dart2JS不知道此代码是否正在使用,并且它不能被树动摇。 Currently any imported Component is in our end executables even when we don't use them. 目前,即使我们不使用它们,任何导入的组件都在我们的最终可执行文件中。
  • We can make it a tiny bit faster by not needing an async action. 我们可以通过不需要异步操作使其快一点。

Tree shaking is the big reason though. 树摇是一个重要原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angular 4中使用权限的最佳方法是什么? - What's the best way to work with permissions in Angular 4? 在Angular2 Dart中设置Router&RouterLink的正确方法是什么 - What's the proper way to set a Router & RouterLink in Angular2 Dart 在Angular中倒退时间的最佳方法是什么 - What is the best way to get time backwards in Angular 显示角度飞镖中对象列表的最佳方法 - Best way to display list of object in angular dart 将谷歌地图 api 加载到 angular2 的最佳方法是什么? - What's the best way to load the google maps api into angular2? 使用 Angular 的 HttpClient 进行同步调用的最佳方法是什么? - What is the best way to make synchronous calls using Angular's HttpClient? 在Angular组件中导入sass变量文件,最好的方法是什么? - importing sass variables file in Angular components, what's the best way? Angular - 在多个组件中包含 html 的最佳方法是什么? - Angular - What's the best way to include html in multiple components? 在angular2中组织路线的最佳方法是什么 - What's the best way to organize Routes in angular2 在角度2(Beta)中将一项服务注入另一项服务的最佳方法是什么? - What's the best way to inject one service into another in angular 2 (Beta)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM