【问题标题】:Migrating host and ComponentResolver from AngularDart 4 to 5将主机和 ComponentResolver 从 AngularDart 4 迁移到 5
【发布时间】: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


    【解决方案1】:

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

    @HostBinding('class.content-item')
    static const bool contentItemClass = true;
    

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

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

    【讨论】:

    • 这是正确的。 ComponentResolver 使用的模式极大地 影响代码大小,因为它有效地防止 Dart 摇树 任何 组件。在幕后,它必须维护一个 Type 到 ComponentFactory 的映射。因此,虽然不太符合人体工程学,但导入生成的文件并直接使用生成的 ComponentFactory 允许编译器从您的应用程序中摇树任何未使用的组件(如果您要导入包含许多组件的库,这很重要,并且您只使用很少)。
    • 今天继续处理这部分并让它工作,感谢您的提示。在这里发布了一个最有效的解决方案:stackoverflow.com/questions/56791433/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多