私はいくつかのシードプロジェクトを見てきましたが、すべてのコンポーネントには、そのコンポーネントから*をエクスポートするindex.tsがあるようです。実際に使用されている場所がどこにもありませんか?
例:https : //github.com/mgechev/angular2-seed/tree/master/src/client/app/%2Bhome
ありがとう
私はいくつかのシードプロジェクトを見てきましたが、すべてのコンポーネントには、そのコンポーネントから*をエクスポートするindex.tsがあるようです。実際に使用されている場所がどこにもありませんか?
例:https : //github.com/mgechev/angular2-seed/tree/master/src/client/app/%2Bhome
ありがとう
回答:
Angular.io V2のアーカイブされた用語集のエントリBarrel
*:
バレルは、複数のモジュールからのエクスポートを1つの便利なモジュールにロールアップする方法です。バレル自体は、他のモジュールの選択したエクスポートを再エクスポートするモジュールファイルです。
heroesフォルダーに3つのモジュールがあるとします。
// heroes/hero.component.ts export class HeroComponent {} // heroes/hero.model.ts export class Hero {} // heroes/hero.service.ts export class HeroService {}
バレルがなければ、消費者は3つのインポートステートメントを必要とします。
import { HeroComponent } from '../heroes/hero.component.ts'; import { Hero } from '../heroes/hero.model.ts'; import { HeroService } from '../heroes/hero.service.ts';
これらのすべてのアイテムをエクスポートするバレルをheroesフォルダー(慣例ではインデックスと呼ばれます)に追加できます。
export * from './hero.model.ts'; // re-export all of its exports export * from './hero.service.ts'; // re-export all of its exports export { HeroComponent } from './hero.component.ts'; // re-export the named thing
これで、消費者はバレルから必要なものをインポートできます。
import { Hero, HeroService } from '../heroes'; // index is implied
Angularスコープのパッケージにはそれぞれ、indexという名前のバレルがあります。
* 注: Angular用語集の最近のバージョンBarrel
から削除されました。
更新 Angularの最新バージョンでは、バレルファイルを以下のように編集する必要があります、
export { HeroModel } from './hero.model'; export { HeroService } from './hero.service'; export { HeroComponent } from './hero.component';
export * from './hero.model.ts'
「 'インポートパスは' .ts 'で終わることはできません」というメッセージが表示されるので、に変更しexport * from './hero.model'
ます。また、Angularがバレルを推奨しない
index.ts
index.js
nodejs で類似しているかindex.html
、Webサイトのホスティングです。
したがって、指定したディレクトリ内import {} from 'directory_name'
を検索しindex.ts
、そこにエクスポートされているものをすべてインポートすると言います。
たとえばcalculator/index.ts
、
export function add() {...}
export function multiply() {...}
できるよ
import { add, multiply } from './calculator';
index.ts
関連するすべてのものをまとめておくのに役立ちます。ソースファイル名を気にする必要はありません。
ソースフォルダー名を使用して、すべてのものをインポートできます。
import { getName, getAnyThing } from './util';
ここで、utilはindex.ts
、4つのファイルすべてを再エクスポートするファイル名ではなく、フォルダー名です。
export * from './util1';
export * from './util2';
export * from './util3';
export * from './util4';