我正忙于将一个大型的Angular4应用程序(刚从Angular2迁移过来)迁移到Angular5。
在应用程序中的几个地方,我们使用一个指令来标记一个div或其他html元素,然后在该组件内部生成这些元素,在本例中,contentItem指令用于指示div应该在布局的中心生成,而footerItem则指示div应该在页面布局的底部生成。
<standard-layout
(print)="handlePrintReport(\$event)"
[hideScrollbars]="true">
<div class="content-main-1" *contentItem>
...
</div>
<div class="content-main-2" *contentItem>
...
</div>
<div class="something-else" *footerItem>
...
<div>
</standard-layout>在StandardLayout内部,
<div
class="content-scroller"
[class.margin-auto]="contentScrollerMarginAuto"
[class.overflow-hidden]="hideScrollbars">
<template ngFor let-item [ngForOf]="contentItems [ngForTrackBy]="trackByFun">
<template [ngTemplateOutlet]="item.template"></template>
</template>
</div>和
@ContentChildren(ContentItemDirective)
List<ContentItemDirective> contentItems;
@ContentChildren(ContentItemFooterDirective)
List<ContentItemFooterDirective> contentItemFooters;在我的ContentItemDirective中,ComponentResolver不再编译,有没有替代它的插件,或者我需要重大的修改才能在Angular5中工作?
@Directive(selector: "[contentItem]")
class ContentItemDirective implements AfterContentInit {
int id = IdProvider.generateIntIndex();
final ViewContainerRef vcRef;
final TemplateRef template;
final ComponentResolver componentResolver;
ContentItemDirective(this.vcRef, this.template, this.componentResolver);
ComponentRef componentRef;
@override
ngAfterContentInit() async {
final componentFactory =
await componentResolver.resolveComponent(ContentItem);
componentRef = vcRef.createComponent(componentFactory);
(componentRef.instance as ContentItem)
..template = template;
}
}然后在我的组件工厂中,host不再编译,我想我可以直接将css类注入转移到StandardLayout中,或者有Angular5方法可以做到这一点吗?
@Component(
selector: "content-item",
template: "",
host: const {
"[class.content-item]": "true",
}
)
class ContentItem {
int id = IdProvider.generateIntIndex();
TemplateRef template;
}如何迁移@Component批注中的ComponentResolver和host属性。
发布于 2019-06-04 13:46:07
组件上的主机属性更改为@HostBindings或@HostListener注释。在这种情况下:
@HostBinding('class.content-item')
static const bool contentItemClass = true;对于解析器,您需要导入工厂本身。导入ContentItem组件的template.dart文件。因此,如果该文件是content_item.dart,则需要导入content_item.template.dart。
然后使用ContentItemNgFactory类型,而不是使用该类型进行查找。
https://stackoverflow.com/questions/56390519
复制相似问题