index.ts全部用作什么? [英] What are all the index.ts used for?
问题描述
我一直在研究一些种子项目,所有组件似乎都有一个index.ts,可以从该组件中导出*.我在任何地方都找不到它的实际用途?
I've been looking at a few seed projects and all the components seem to have a index.ts that exports * from that component. I can't find anywhere what it's actually used for?
例如 https://github.com/mgechev/angular2-seed/tree/master/src/client/app/%2Bhome
谢谢
推荐答案
来自
桶是一种将多个模块的出口汇总到一个模块中的方法
便利模块.枪管本身是重新导出的模块文件
选择其他模块的导出.
A barrel is a way to rollup exports from several modules into a single
convenience module. The barrel itself is a module file that re-exports
selected exports of other modules. 想象一下英雄文件夹中的三个模块: Imagine three modules in a heroes folder: 没有桶,消费者将需要三个导入语句: Without a barrel, a consumer would need three import statements: 我们可以将桶添加到heroes文件夹(按惯例称为索引)
导出所有这些项目: We can add a barrel to the heroes folder (called index by convention)
that exports all of these items: 现在,消费者可以从桶中进口所需的东西. Now a consumer can import what it needs from the barrel. 每个Angular作用域包都具有一个名为index的桶. The Angular scoped packages each have a barrel named index. 另请参见期望:无法解析所有参数 * 注意: 更新
对于最新版本的Angular,桶形文件应按以下内容进行编辑, UPDATE
With latest versions of Angular, barrel file should be edited as below,
这篇关于index.ts全部用作什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
// heroes/hero.component.ts
export class HeroComponent {}
// heroes/hero.model.ts
export class Hero {}
// heroes/hero.service.ts
export class HeroService {}
import { HeroComponent } from '../heroes/hero.component.ts';
import { Hero } from '../heroes/hero.model.ts';
import { HeroService } from '../heroes/hero.service.ts';
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
Barrel
已从 Angular词汇表的最新版本.
export { HeroModel } from './hero.model';
export { HeroService } from './hero.service';
export { HeroComponent } from './hero.component';