无法解析[Component]的所有参数

时间:2017-12-03 10:59:12

标签: angular angular-components

在我的通用组件中,我有这个波浪形的说法: ' Angular:无法解析[path / base-collection.component.ts:([object.Object],[object.Object],?,[object.Object])] <中的BaseCollectionComponent的所有参数/强>

以下是组件的代码:

import {BaseRepositoryService} from '../../services/base-repository.service';
import {Component, ComponentRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
import {BaseComponent} from '../base-component/base.component';
import {CreateCollectionItemModel} from '../../models/create-collection-item.model';
import {ComponentFactoryService} from '../../services/component-factory.service';
import {FrontendSelectorsIds} from '../../globals/frontend-selectors-ids';
import {BaseCollectionItemComponent} from '../base-collection-item/base-collection-item.component';

@Component({
  selector: FrontendSelectorsIds.BaseCollectionSelector,
  templateUrl: './base-collection.component.html',
  styleUrls: ['./base-collection.component.less']
})
export class BaseCollectionComponent<TypeModel> {

  @ViewChild('collectionItemsContainer', {read: ViewContainerRef}) collectionItemsContainer: ViewContainerRef;
  model: TypeModel[];
  components: ComponentRef<BaseComponent<TypeModel>>[];

  constructor(public repository: BaseRepositoryService<TypeModel>,
              public factoryService: ComponentFactoryService<TypeModel, BaseComponent<TypeModel>>,
              public componentType: Type<BaseCollectionItemComponent<TypeModel>>,
              public createCollectionItemModel?: CreateCollectionItemModel) {
    this.components = [];
  }

  loadModel(): void {
    this.repository.getAll()
      .then(results => {
        this.model = results;
        this.onLoadModelComplete();
      });
  }

  destroyChildren(): void {
    if (this.components) {
      for (const component of this.components) {
        component.destroy();
      }
    }
  }

  onLoadModelComplete() {
    this.createComponents(this.model);
  }

  mapResultsToCollection(): void {

  }

  createComponents(model: TypeModel[]): void {
    this.factoryService.setRootViewContainerRef(this.collectionItemsContainer);

    for (const item of model) {
      const newComponent = this.factoryService.addDynamicComponentWithModel(item, this.componentType);
      this.components.push(newComponent);
    }
  }
}

基本上,这个组件用于为集合注入子列表项组件。

首先我没有真正得到错误指向我的地方,但我认为它指向构造函数,因为还有4个参数。问号指向第3个位置,它是我在子类中提供的类型参数。

我查找了同类的现有问题,但没有提供的解决方案对我有用。

发出装饰器元数据的选项(切换真/假)没有帮助。

有什么建议吗? 如果需要更多代码,请告诉我。 提前谢谢!

更新

用作参数的类(声明它们的方式):

存储库

@Injectable()
export class ProductRepositoryService extends BaseRepositoryService<Product>
@Injectable()
export abstract class BaseRepositoryService<TypeModel>

这是与我的bnackend api沟通的服务基地。它有由Angular注入的HttpClient。没什么特别的。

factoryService

@Injectable()
export class ComponentFactoryService<TypeModel, TypeCollectionItem extends BaseCollectionItemComponent<TypeModel>>

此服务负责产生组件。

组件类型

export class ProductItemComponent extends BaseCollectionItemComponent<Product> implements OnInit

这是我用来显示集合列表项的基本组件。

createCollectionItemModel

export class CreateCollectionItemModel

这只是一个包含数据的类。

在我看来,整个问题与第3个参数有关,我提供的工厂服务类型。我认为存在某种不匹配。我正在努力让它发挥作用。问题部分是通常传递所需组件的类型来实例化。以下代码使其全部工作(至少在开发模式下):

这是BaseCollectionComponent类调用(来自原始帖子):

createComponents(model: TypeModel[]): void {
    this.factoryService.setRootViewContainerRef(this.collectionItemsContainer);

    for (const item of model) {
      const newComponent = this.factoryService.addDynamicComponentWithModel(item, this.componentType);
      this.components.push(newComponent);
    }
  }

以下是factoryService完整代码:

@Injectable()
export class ComponentFactoryService<TypeModel, TypeCollectionItem extends BaseCollectionItemComponent<TypeModel>> {

  rootViewContainer: ViewContainerRef;

  constructor(@Inject(ComponentFactoryResolver) private _factoryResolver: ComponentFactoryResolver) {
  }

  setRootViewContainerRef(viewContainerRef: ViewContainerRef) {
    this.rootViewContainer = viewContainerRef;
  }

  addDynamicComponentWithModel(model: TypeModel, componentType: Type<TypeCollectionItem>): ComponentRef<TypeCollectionItem> {
    const factory = this._factoryResolver.resolveComponentFactory(componentType);
    const component = factory.create(this.rootViewContainer.parentInjector);

    this.rootViewContainer.insert(component.hostView);
    component.instance.model = model;

    return component;
  }
}

我实现它的方式看起来有些泛滥。我希望这将加起来理解这里的错误。

2 个答案:

答案 0 :(得分:1)

它不是@Injectable,因此您无法使用依赖注入。

答案 1 :(得分:0)

嗯,事实证明,BaseCollectionComponent构造函数的第三个参数确实导致了问题。 我只是开始逐个拔出所有代码位,看看问题是否消失了。我删除了类型&lt; ...&lt; ...&gt;&gt;参数(thew 3rd one)而我改变了这个:

 createComponents(model: TypeModel[]): void {
    this.factoryService.setRootViewContainerRef(this.collectionItemsContainer);

    for (const item of model) {
      const newComponent = this.factoryService.addDynamicComponentWithModel(item, this.componentType);
      this.components.push(newComponent);
    }
  }

到此:

    createComponents(model: TypeModel[], type: any): void {
    this.factoryService.setRootViewContainerRef(this.collectionItemsContainer);

    for (const item of model) {
      const newComponent = this.factoryService.addDynamicComponentWithModel(item, type);
      this.components.push(newComponent);
    }
  }
问题消失了。似乎传递了类型&#39;类型的参数&#39;确实以某种方式混淆了角度编译器。我还是不明白为什么会这样。如果有人可以指出它?无论如何,感谢大家的建议。