内部の自動インポートすべて使用されるコンポーネント私は活字体でAngular2に非常に新たなんだと私は今、長い時間のために解決することはできませんよ問題を抱えている:角度2 RC1 - 基本コンポーネント
私が提供したいですユーザーはビュー内で小さなコンポーネントツールキットを使用して(実際の)小さなアプリケーションを構築します。したがって、既にインポートされているかどうかにかかわらず、子要素のそれぞれを初期化してチェックするときに、その子要素をすべて調べることができるアプリケーションコンポーネントが必要です。そうでなければ、その使用を可能にするためにそれをインポートします。
<test-auto-import>
<!-- freely use custom components, because the test auto import element
will automatically take care of the imports when initializing -->
<some-custom control="that is acutally not imported"></some-custom>
<another-unimported></another-unimported>
</test-auto-import>
これを解決するための基本的な考え方は次のとおりです:
import { Component, OnInit } from '@angular/core'
@Component({
selector: 'test-auto-import',
templateUrl: 'test-auto-import.html',
styleUrls: ['test-auto-import.css']
})
class TestAutoImportComponent implements OnInit
{
ngOnInit() {
// run through children and getting their tag names
let app = document.getElementsByTagName('test-auto-import');
let children = app.childNodes;
let importNames = '';
// so iterate now:
for(let i =0; i< children.length; i++)
{
let child = children[i];
// ---------------
// todo:
// 1. import the component from generate path by tag name
// 2. get the @Component directives array
// 3. check whether the name is in it
// 4. if not, add it
// ---------------
}
}
だから、基本的には、ステップ1と2(多分これは私がacchieveしたいものである - HTML /コードでそれを言って
4も同様)は、何らかの方法で解決できなかった重要なステップです。私は本当に必死になってる:(
を任意のヘルプを強くお勧めし 感謝を事前にみんなで
注意!:タグ名は正しいパスを定義するのに十分であろうカスタム命名/構造化規則のおかげでコンポーネントは、タグ名などして、「私のテストは、」静的パス「../elements/my-test/my-test.component.ts」につながる。
お返事ありがとうございました!私は2番目のステップで、Componentの配列をインポートしています。説明してください。何が起こっているのですか?(...)またはPLATFORM_DIRECTIVESはどこから来ますか?再度、感謝します。 – jlang
'export const MY_DIRECTIVES ...'を持つファイルは、すべてのコンポーネントをインポートし、それらをすべて配列にリストする必要があります。ユーザは 'bootstrap(...)'を含むファイルに 'MY_DIRECTIVES'をインポートし、最後のコード行を' bootstrapp(AppComponent、[provide(PLATFORM_DIRECTIVES ...)] ')に追加する必要があります。 –
この方法の使用には欠点があります。 Angularチームはツリーの揺れに取り組んでいますが、このアプローチではうまく動作せず、必要な出力サイズを大きくする原因になることは間違いありません。また、コンポーネントのユーザーがコンポーネントだけをインポートする方法もサポートする必要があります彼らは実際に使いたいと思う。これはAngularで行われる方法なので、とにかく慣れています。 –