2017-06-15 57 views
1

我在Angular中加載動態組件加載了一個奇怪的錯誤。 我創建一個表單步進器,有5個步驟。每一步都是一個組件,並且有一個通用的組件來在需要時注入它們。Angular2:動態加載組件錯誤

class CreateComponent { 
    // List of components i want to inject 
    private _ContainerContractComponent = [ 
     ContractCivilStatusComponent, 
     ContractLegalResponsableComponent, 
     ContractBulletinComponent, 
     ContractPaymentComponent, 
     ContractSocialSecurityComponent 
    ]; 
    // List of var i want to inject 
    private _currentStudent: StudentInterface; 
    private _candidature: CandidatureInterface; 
    private _childObserver: Subject<any> = new Subject(); 
    // My step (numberBetween<1,5>) 
    private _stepperStep: number; 

    // My loading function 
    displayContractPartComponent() { 
     let componentFactory = this._componentFactoryResolver.resolveComponentFactory(
      this._ContainerContractComponent[this._stepperStep - 1] 
      ); 
     this.dynamicComponentContainer.clear(); 
     let componentRef = this.dynamicComponentContainer.createComponent(componentFactory); 
     componentRef.instance.candidature = this._candidature; 
     componentRef.instance.student = this._currentStudent; 
     componentRef.instance.childObserver = this._childObserver; 
    } 
} 

對於每個加載的組件我傳遞3 var:candidateature,student,childObserver。

一切工作的好直到今天早上,編譯器總是彈出我這個錯誤:

Error in CreateComponent: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.

Type argument candidate 'ContractBulletinComponent' is not a valid type argument because it is not a supertype of candidate 'ContractCivilStatusComponent'.

Property '_model' is missing in type 'ContractCivilStatusComponent'.

這很奇怪,因爲最後一行(財產「_model」 ...)改變complilation的每一輪和名稱組件也是(這裏是'ContractBulletinComponent')。

我今天早上一直在尋找,但我不知道什麼時候可以來,所以也許你們中的任何人都可以幫助我。 感謝的

+0

這段代碼與泛型有關,但我沒有在你發佈的代碼中,你確定這是失敗的地方嗎? – toskv

+0

該錯誤表明您嘗試使用ContactCulletinComponent類型,其中ContractCivilStatusComponent類型是預期的。而且自從第一次不是第二次編譯的孩子失敗。我沒有看到你的代碼在哪裏可能發生。 – toskv

+1

你可以嘗試'讓componentRef:ComponentRef =' – echonax

回答

0

我發現我的解決方案,該probem是,當我宣佈我的組件的排列:

private _ContainerContractComponent = [ 
    ContractCivilStatusComponent, 
    ContractLegalResponsableComponent, 
    ContractBulletinComponent, 
    ContractPaymentComponent, 
    ContractSocialSecurityComponent 
]; 

我需要添加型陣列的任何它,像這樣:

private _ContainerContractComponent: Array<any> = [ 
    ContractCivilStatusComponent, 
    ContractLegalResponsableComponent, 
    ContractBulletinComponent, 
    ContractPaymentComponent, 
    ContractSocialSecurityComponent 
];