Tuesday, March 20, 2018

a good pattern for barrels

A good pattern for barrel files in an Angular 4 app is to make an index.ts file be an index of what is in the folder it sits in and not an index of what imports the files in the folder have in common. Do not try to use an index at a folder to clean up what is in that particular folder. Instead let it represent the folder when files and different folders call out to things in that folder. When referencing an index file in another folder, you make make an import devoid of the actual index name/word such that...

import * as Whatever from '../../yin/yang/index';

 
 

...may be be simplified as...

import * as Whatever from '../../yin/yang';

 
 

Addendum 3/21/2018: The as variable should probably be in camel case and not Pascal case and thus be whatever and not Whatever.

No comments:

Post a Comment