2017-09-18 3 views
3

私はアプリケーションを構築しており、開発者は特定の部分をどのコンポーネントでレンダリングするかを指定できます。私は、ユーザーにインターフェイスを実装する必要があることを知りたがっているが、タイピングを正しく書く方法がわからない。クラスコンストラクタを使用してクラスインスタンスのインタフェースを定義する方法

export interface ICustomComponent { 
    templateObject: any; 
} 

export class MyComponent implements ICustomComponent { 
} 

export class MyLib { 
    constructor(
     private yourComponent: ICustomComponent 
    ) {} 
} 

new MyLib(MyComponent); <== Fails 

私は角を持つコードを書いています、と私は、new演算子を実行しますが、解決し、そのコンポーネントを構築する角度を任せることはできません。

Here私の問題を示す例。

この問題を処理するにはどうすればよいですか?

+0

あなたはここに 'new MyLib(...);'を渡しますか? 'new MyLib(new MyComponent());)やクラスへの参照? –

+0

https://angular.io/guide/dynamic-component-loader#resolving-componentsその例は問題を説明することだけです – tom10271

+0

これで 'MyComponent'をcomponentFactoryResolver.resolveComponentFactory(MyComponent);に渡しますか? –

答えて

2

MyLibは、クラスのコンストラクタではなく、クラスのインスタンスを期待しているので、あなたはクラスのコンストラクタのためのインタフェースを定義し、それがICustomComponentインターフェースでインスタンスを返すように指定する必要があります。

interface ICustomComponent { 
    templateObject: any; 
} 

interface ICustomComponentConstructor { 
    new (...deps: any[]): ICustomComponent; 
} 

そして、あなたはそれを使用することができますこのように:

export class MyComponent implements ICustomComponent { 
    templateObject: any; 
} 

export class MyLib { 
    constructor(private yourComponent: ICustomComponentConstructor) { 
    } 
} 

new MyLib(MyComponent); 

あなたはクラスのコンストラクタとインスタンスhereするためのインタフェースについて読むことができます。

関連する問題