Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Use a Barrel File for all Component Imports

I am currently on Angular 5.

I have created a barrel file for all the common imports I use in my component files. The question that I have is - is this a good idea or bad idea in regards to lazy loading, tree shaking, or anything else I didn't think of such as AOT? Or is this approach completely fine and won't affect load time or anything else negatively. If you could shine some light on the reason too that would be appreciated.

Examples:

  common-component-imports.ts:
    export { AppState, Event, EventPriority, EventType, Page, Unit, User, WindowSettings } from '../models/index';
    export { Component, ElementRef, Input, ViewChild, ViewChildren } from '@angular/core';
    export { Config, IonicPage, Loading, LoadingController, Modal, ModalController, Nav, NavController, NavParams, Platform, Toast, ToastController, ViewController } from 'ionic-angular';
etc../

and than in my components I am doing this:

event.ts
import {
  Component,
  ElectronService,
  Event,
  AppState,
  IonicPage,
  NavController,
  NavParams,
  Store,
 } from '../shared/common-component-imports';

and I import like that on several more files besides just event.ts.

like image 903
RooksStrife Avatar asked Nov 29 '17 15:11

RooksStrife


People also ask

What is barrel imports in Angular?

A barrel, in Angular, is simply a module file which provides a centralized place for exporting components, interfaces, services etc. In turn, in your main module file, you make all imports from that single source that you defined in the barrel file.

What is barrel import?

A barrel enables us to consolidate, or roll up, exports from multiple files or modules into one single module. Barrels streamline imports, simplify exports, and help us avoid a lot of clutter in our codebase. Let's look at how barrels can simplify imports, both mentally and visually.

Why do I need index TS?

ts allows you to organize your imports. This file can be used to import multiple modules from other folders and re-export them so that they can be consumed by the other modules more easily. The modules that were re-exported in Index.

What is imports array in Angular?

Export array − This is used to export components, directives, and pipes which can then be used in other modules. Import array − Just like the export array, the import array can be used to import the functionality from other Angular JS modules.

Why do we use barrel files in angular?

While we can organize a lot of things with the help of Angular modules, I still prefer to also use a few barrel files throughout the application. They greatly simplify the imports and make them look clearer.

What is a barrel file and how to use it?

Barrel files: to use or not to use? - Blog by Adrian Fâciu Barrel files: to use or not to use? First of all, what are these barrel files? Using ES2015 modules, we have files from which we export one or more things. Barrel files are a way to re-export all or some of these from one, single, convenient place.

What are barrel files in ES2015?

Using ES2015 modules, we have files from which we export one or more things. Barrel files are a way to re-export all or some of these from one, single, convenient place. You can understand better what they are and how they are used by looking at this short example from the aswesome TypeScript deep dive book.

What is the best way to name an angular routing module?

The Angular official style guide is pretty straight forward in providing the best way to name the files that contain the building blocks of Angular. Routing Modules should be suffixed with -routing.module.ts;


1 Answers

Barrels are a good idea in general but have no impact on lazy loading. https://angular.io/guide/glossary#barrel (barrel has been removed from the glossary 2018)

UPDATE: "Consider creating a "barrel" file that gathers all the interceptor providers into an httpInterceptorProviders array, starting with this first one, the NoopInterceptor." https://angular.io/guide/http

Style Guide" with NgModules (2016-09-27) "StyleGuide explains recommended conventions for NgModules. Barrels now are far less useful and have been removed from the style guide; they remain valuable but are not a matter of Angular style. Also relaxed the rule that discouraged use of the @Component.host property." https://angular.io/guide/change-log

like image 99
mruanova Avatar answered Oct 22 '22 15:10

mruanova