微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误:HighlightDirective 不能用作入口组件吴升级

如何解决错误:HighlightDirective 不能用作入口组件吴升级

我正在使用 NgUpgrade 模块将旧的 AngularJS 应用程序升级到 Angular。升级组件似乎工作正常,但我一直在努力让指令工作。在我无法升级自己的组件后,我从 Angular 教程中复制了 Highlight 指令并尝试使用它。

因此,我创建了指令,将其添加到指令模块中,然后将其降级以在 AngularJS 中使用。

执行此操作时出现错误

错误:未找到 HighlightDirective 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

好的编译器先生,不用担心,我会将它添加EntryComponents 中。刷新页面并...

未捕获的错误:HighlightDirective 不能用作入口组件。

现在我被难住了,你是如何做到这一点的?

突出显示指令

@Directive({
    selector: '[highlight]'
})
export class HighlightDirective {
    constructor(el: ElementRef) {
        el.nativeElement.style.backgroundColor = 'yellow';
    }
}

在 HTML 中的使用

<div highlight>

指令.模块

@NgModule({
    imports: [
        CommonModule,MiscDirectivesModule
    ],entryComponents: [
        DashboardBoxSmallComponent,// Components work fine,directives do not
        HighlightDirective
    ],declarations: [
        DashboardBoxSmallComponent,HighlightDirective
    ],exports: [
        HighlightDirective
    ]
})
export class DirectivesModule {
    constructor() {
    }
}

指令.模块.AJS 这是我的闪亮 Angular 组件为 AngularJS 降级的地方。 时髦的降级代码来自 here,显然“目前,您无法降级属性指令。”。

const allowAttribute = directiveFactory => [
    '$injector',($injector: angular.auto.IInjectorService) =>
        Object.assign($injector.invoke(directiveFactory),{ restrict: 'EA' }),];

const downgradeAttributeComponent = info => allowAttribute(downgradeComponent(info));

angular
  .module("app.directives",[...])
  .directive("highlight",downgradeAttributeComponent({ component: HighlightDirective }))

应用模块

@NgModule({
    imports: [
        browserModule,UpgradeModule,ServicesModule,DirectivesModule,// This is the module shown above
        FiltersModule
    ],declarations: [],entryComponents: [],exports: []
})
export class AppModule {
    constructor(private upgrade: UpgradeModule) {
    }

    ngdobootstrap() {
        this.upgrade.bootstrap(document.documentElement,[moduleName],{ strictDi: true });
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。