index.ts全部用作什么? [英] What are all the index.ts used for?

查看:989
本文介绍了index.ts全部用作什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一些种子项目,所有组件似乎都有一个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:

// heroes/hero.component.ts
export class HeroComponent {}

// heroes/hero.model.ts
export class Hero {}

// heroes/hero.service.ts
export class HeroService {}

没有桶,消费者将需要三个导入语句:

Without a barrel, a consumer would need three import statements:

import { HeroComponent } from '../heroes/hero.component.ts';
import { Hero }          from '../heroes/hero.model.ts';
import { HeroService }   from '../heroes/hero.service.ts';

我们可以将桶添加到heroes文件夹(按惯例称为索引) 导出所有这些项目:

We can add a barrel to the heroes folder (called index by convention) that exports all of these items:

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

现在,消费者可以从桶中进口所需的东西.

Now a consumer can import what it needs from the barrel.

import { Hero, HeroService } from '../heroes'; // index is implied

每个Angular作用域包都具有一个名为index的桶.

The Angular scoped packages each have a barrel named index.

另请参见期望:无法解析所有参数

* 注意: Barrel已从 Angular词汇表的最新版本.

更新 对于最新版本的Angular,桶形文件应按以下内容进行编辑,

UPDATE With latest versions of Angular, barrel file should be edited as below,

export { HeroModel } from './hero.model';  
export { HeroService } from './hero.service'; 
export { HeroComponent } from './hero.component';

这篇关于index.ts全部用作什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆