【发布时间】:2019-05-31 07:21:52
【问题描述】:
我正忙着将一个大型 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>
在标准布局内,
<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;
}
TLDR,我如何迁移ComponentResolver 和@Component 注释中的host 属性。
【问题讨论】:
标签: angular dart angular-dart