Angular 6 自定义库没有 ComponentFactoryResolver 的提供程序

2023-11-25

我从 NG4 -> NG6 重写库(使用 angular-cli 6)。这个库改变了动态角度分量,所以我使用ComponentFactoryResolver我被困住了。当我添加ComponentFactoryResolver到构造函数:

constructor(
    private componentFactoryResolver: ComponentFactoryResolver
) {}

并在目标应用程序中构建 lib (成功)(我从本地文件将我的 lib 添加到 package.json 中),但出现此错误:

StaticInjectorError(AppModule)[MyLibComponent-> ComponentFactoryResolver]: 
  StaticInjectorError(Platform: core)[MyLibComponent-> ComponentFactoryResolver]: 
    NullInjectorError: No provider for ComponentFactoryResolver!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:979)
    at resolveToken (core.js:1232)
    at tryResolveToken (core.js:1182)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077)
    at resolveToken (core.js:1232)
    at tryResolveToken (core.js:1182)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077)
    at resolveNgModuleDep (core.js:9238)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:9919)

Without ComponentFactoryResolver其他工作正常

完整组件:

import { Component, ComponentFactoryResolver, AfterContentInit, ViewChild, Inject, Input, InjectionToken } from '@angular/core';

import { AbHostDirective } from './../directives/ab-host.directive';

export const AB_COMPONENTS = new InjectionToken('AB_COMPONENTS');

@Component({
  selector: 'lib-ab',
  template: `<ng-template lib-ab-host></ng-template>`,
  styles: []
})

export class MyLibComponent implements AfterContentInit {
  @ViewChild(AbHostDirective) abHost: AbHostDirective;
  @Input() globalConfiguration: any;
  @Input() domain = '';

  constructor(
    @Inject(AB_COMPONENTS) private $configuredComponents: any,
    private componentFactoryResolver: ComponentFactoryResolver
  ) {}

  ngAfterContentInit() {
    const currentConfiguration = this.$configuredComponents[this.domain];

    if (!currentConfiguration) {
        console.error('No component for: ', + this.domain);
        return;
    }

    const randomIndex      = Math.floor(Math.random() * currentConfiguration.length);
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(currentConfiguration[randomIndex]);
    const viewContainerRef = this.abHost.viewContainerRef;
    viewContainerRef.clear();
    const componentRef = viewContainerRef.createComponent(componentFactory);
    const instance     = <any>componentRef.instance;
    instance.globalConfiguration     = this.globalConfiguration;
  }
}

我遇到过同样的问题。这勾解决了我的问题:

https://github.com/angular/angular/issues/20598

尝试将其添加到您的 angular.json 文件中:

"build": {
    "options": {
        "preserveSymlinks": true,
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 6 自定义库没有 ComponentFactoryResolver 的提供程序 的相关文章

随机推荐

  • 如何使 findpeak 函数检测负峰值而不是正峰值

    我想知道我该怎么做findpeak函数检测负峰而不是正峰 它检测下图所示的红色峰 我需要检测蓝色峰 有什么想法吗 非常感谢 如前所述 你必须使用 data 这是一个例子 x 0 1e 3 5 pi t 0 length x 1 1e 3 y
  • 支持多界面,但主屏幕只有单一界面,在 iOS8 + iPhone 中不工作

    我有如下所示的视图结构 HomeView Support only portrait mode V View1 Support all orientation V View2 Support all orientation 问题 当我com
  • 首先使用代码在实体框架中建模关系问题

    我试图首先在实体框架中学习代码 但在建模关系时遇到问题 它是一个基本的人力资源数据库 因此有两个实体 员工和部门 员工属于一个部门 该部门有一名团队管理员和一名经理 他们实际上都是员工 我尝试使用以下方法对此进行建模 EMPLOYEE pu
  • BackgroundWorker & Timer,只读取日志文件的新行?

    我的应用程序写入一个日志文件 当前使用log4net 我想设置一个计时器和一个后台工作人员来读取日志文件并将其内容打印到我的表单中的某些控件中 同时写入日志文件 我无法使用文件系统观察者类因为似乎被破坏了 有时事件 改变 会触发 有时则不会
  • 删除 CR 行终止符

    首先我想说我已经读过这个帖子但是我仍然有问题CR line terminators 有一个文件叫build test sh 我编辑于leafpad它可以直接显示在Vim cp moonbox llvm 2 9 lib Transforms
  • 如何在B类中定义A类,在A类中定义B类?

    我有两种类型 一种类型 A 一种类型 B 问题类型 A 包含类型 B 类型 B 包含类型 A 这样的事情是行不通的 type typeA record test1 typeB end type typeB record test2 type
  • 使用哪款 Android 手机进行开发...? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 我计划将一些游戏从 iPh
  • 免费的 UML 工具,非常适合 .NET [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心以获得指导 谁能推荐一款您曾经使用过并
  • 错误:TypeError:$(...).dialog 不是函数

    我在获取对话框作为基本功能时遇到问题 这是我的 jQuery 源导入 Html
  • 如何使用硬件视频缩放器?

    现代显卡具有硬件视频缩放器 例如作为 AMD Avivo NVIDIA PureVideo 或 Intel ClearVideo 的一部分 例如 AMD 的Avivo 白皮书 says 图像输出缩放器支持最多 6 个垂直滤波器抽头及以上 至
  • 基于区域设置的python csv列表分隔符

    如何使用Python检测用户机器中的列表分隔符 需要在用户计算机上创建 CSV 文件 并且必须自动检测列表分隔符 以便 Excel 可以读取 CSV 文件 我发现 Excel 从 区域选项 gt 数字 gt 列表分隔符 中获取 CSV 元素
  • d3 重置范围后保留比例/平移

    我有一个
  • 如何将组件引用传递给 onPress 回调?

    我确实使用 onPress 处理程序 渲染了以下类型的列表 我意识到 onPress 处理程序是无用的 因为我无法获取按下种族的参考 我收到 ref 未定义错误 var races Engine possibleRaces function
  • 来自不受信任的 UID 的呼叫

    当我在 Android Studio 中运行 UIAutomator 时 有时会出现崩溃 W ActivityManager Crash of app com example testsample running instrumentati
  • 替代 NSData 已弃用的 dataWithContentsOfMappedFile

    So id dataWithContentsOfMappedFile NSString 路径自 iOS 5 0 以来显然已被弃用 在我看来 我应该避免使用它 但是我应该使用什么来代替呢 我使用 mmap 来创建内存映射文件 它适用于 iOS
  • 如何以编程方式注册 JSF 托管 bean?

    我想以编程方式 从 Servlet init 内 将托管 Bean 类注册 添加到应用程序范围中 我如何使用 JSF 1 2 做到这一点 对于所有范围的托管 bean 不太可能从您的应用程序中以编程方式执行此操作 BalusC 已经指出了如
  • 在 PHP 上启用 SOAP

    我想在我的 PHP5 Centos Apache 服务器上启用 SOAP 我可以完全访问服务器 我的 php ini 文件包含以下内容 当我查看 phpinfo 时 它没有引用 SOAP 有什么建议么 谢谢 soap Enables or
  • 协议和Json Wire协议有什么区别

    Protocol 定义通过网络交换数据的方法的标准 如果浏览器想要与服务器通信 它必须创建 HTTP 请求并将该 HTTP 请求发送到服务器以传达其资源和选项请求 服务器接收请求并处理它并执行必要的操作并创建 HTTP 响应以发送到浏览器
  • 使用套接字将数据从 Node.js 发送到 Java

    我正在尝试通过套接字将数据从node js 发送到Java 我四处搜寻 但没有什么真正有用的 我习惯了 socket io 但在这种情况下它似乎不太适合这个 似乎 Node js 的所有套接字扩展并不真正适合发送消息 而是适合监听消息并应答
  • Angular 6 自定义库没有 ComponentFactoryResolver 的提供程序

    我从 NG4 gt NG6 重写库 使用 angular cli 6 这个库改变了动态角度分量 所以我使用ComponentFactoryResolver我被困住了 当我添加ComponentFactoryResolver到构造函数 con