首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将主机和ComponentResolver从AngularDart 4迁移到5

将主机和ComponentResolver从AngularDart 4迁移到5
EN

Stack Overflow用户
提问于 2019-05-31 15:21:52
回答 1查看 98关注 0票数 2

我正忙于将一个大型的Angular4应用程序(刚从Angular2迁移过来)迁移到Angular5。

在应用程序中的几个地方,我们使用一个指令来标记一个div或其他html元素,然后在该组件内部生成这些元素,在本例中,contentItem指令用于指示div应该在布局的中心生成,而footerItem则指示div应该在页面布局的底部生成。

代码语言:javascript
复制
<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内部,

代码语言:javascript
复制
<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>

代码语言:javascript
复制
@ContentChildren(ContentItemDirective)
List<ContentItemDirective> contentItems;

@ContentChildren(ContentItemFooterDirective)
List<ContentItemFooterDirective> contentItemFooters;

在我的ContentItemDirective中,ComponentResolver不再编译,有没有替代它的插件,或者我需要重大的修改才能在Angular5中工作?

代码语言:javascript
复制
@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方法可以做到这一点吗?

代码语言:javascript
复制
@Component(
    selector: "content-item",
    template: "",
    host: const {
        "[class.content-item]": "true",
    }
)
class ContentItem {

    int id = IdProvider.generateIntIndex();

    TemplateRef template;
}

如何迁移@Component批注中的ComponentResolverhost属性。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-04 13:46:07

组件上的主机属性更改为@HostBindings或@HostListener注释。在这种情况下:

代码语言:javascript
复制
@HostBinding('class.content-item')
static const bool contentItemClass = true;

对于解析器,您需要导入工厂本身。导入ContentItem组件的template.dart文件。因此,如果该文件是content_item.dart,则需要导入content_item.template.dart。

然后使用ContentItemNgFactory类型,而不是使用该类型进行查找。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56390519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档