Angular 2 Barrels
In Angular 2, I'm trying to get barrels to work as described in the documentation.
The official Angular 2 style guide talks about using barrels to aggregate and shorten import
statements.
I'm finding out that for some barrels, I have to specify the index
JavaScript file name on the import when I shouldn't have to.
Barrel Example
(modify the app/app.component.ts file on line 12)
After have encountered this in my actual project (running under ASP.NET) I have created a Plunker to demonstrate the problem where I modified the Tour of Heroes to use barrels.
In app/app.component
, the basic way to import is like this:
import { HeroService } from './hero.service';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
But, to use a barrel instead, the import definition would look like this:
import {
HeroService,
HeroesComponent,
HeroDetailComponent
} from '../app';
The from '../app';
line indicates a file with the name of index.ts
that contain the exported/imported components:
// app/index.ts
export * from './hero-detail.component';
export * from './hero.service';
export * from './heroes.component';
But this doesn't work for me in all cases. The only way I've gotten this to work correctly is by explicitly including the index
file name:
import {
HeroService,
HeroesComponent,
HeroDetailComponent
} from '../app/index'; // have to indicate 'index'
How can I get this to work where the index.js
file name is implied?