加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 6 custom lib没有ComponentFactoryResolver的提供程序

发布时间:2020-12-17 17:11:24 所属栏目:安全 来源:网络整理
导读:我从NG4重写了库 – NG6(使用angular-cli 6).这个lib改变了dynamicli角度组件,所以我使用ComponentFactoryResolver,在这里我卡住了.当我将ComponentFactoryResolver添加到构造函数时: constructor( private componentFactoryResolver: ComponentFactoryReso
我从NG4重写了库 – > NG6(使用angular-cli 6).这个lib改变了dynamicli角度组件,所以我使用ComponentFactoryResolver,在这里我卡住了.当我将ComponentFactoryResolver添加到构造函数时:

constructor(
    private componentFactoryResolver: ComponentFactoryResolver
) {}

并在目标应用程序中构建lib(成功)(我在本地文件中添加我的lib.json中的lib)我有这样的错误:

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)

没有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,}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读