gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:27:53 25 4
gpt4 key购买 nike

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 时需要注意我遗漏了什么?

最佳答案

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

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

摇树是主要原因。

关于angular - 在 Angular 4 Dart 中获取 ComponentFactory 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46957334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com