Monday, March 19, 2018

Use the export keyword when creating a barrel file in an Angular 4 app.

This is a distinction to what I mention here. If we have whatever.ts that starts out like so...

import { Routes, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';

 
 

...then perhaps an index.ts file would look like so in the name of abstracting this stuff out:

export { Routes, RouterModule } from '@angular/router';
export { NgModule } from '@angular/core';

 
 

That would then be looped in back at whatever.ts like so:

import { Routes, RouterModule, NgModule } from './index';

 
 

You could also do this...

import * as Whatever from './index';

 
 

But that means you would have to call out to RouterModule as Whatever.RouterModule instead of just RouterModule.

No comments:

Post a Comment