The Angular Style Guide says of Import line spacing:
Looking at many Angular projects including Angular itself the convention is to import Angular (@angular
) modules first, then third-party modules (e.g. AngularFire2) and then our own modules (Services, Components, etc.) e.g. ./some-service.ts
.
Again it looks like it is the convention to import Services, then Models and then Components.
But what about Interfaces and Pipes, etc.? And what is the convention for importing 'nameless' or wildcard modules, e.g. the Firebase SDK or RxJs operators?
For example:
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import {
AngularFireDatabase,
FirebaseListObservable
} from 'angularfire2/database';
import * as firebase from 'firebase/app';
import 'rxjs/add/operator/take';
...
import { Injectable } from '@angular/core';
import {
AngularFireDatabase,
FirebaseListObservable
} from 'angularfire2/database';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';
// `GridMetadata` is an Interface
import { GridMetadata } from './grid-metadata';
...
Yes. Order is important if one module depends on another.
first.module.ts Import the SharedModule into any module where you need to use shared modules or components. As you can see in the image, App is the name you've passed in the app. component.
Or Simply, to use any methods defined in other classes, we need to import it first.. So its understandable. And also in the NgModule decorative, in the import statement, we import them.. Because an angular module (as a feature) defines what are the modules that can be used by its components, services etc..
Every Angular application has at least one NgModule class, the root module, which is conventionally named AppModule and resides in a file named app. module. ts . You launch your application by bootstrapping the root NgModule.
Actually, looking at the Angular codebase and with a better understanding of the guidelines, there is an answer.
This just needs an example to explain it:
// Core imports
import { TestBed, async, inject } from '@angular/core/testing';
// Third party imports
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/of';
// Application imports
import { AuthService } from './auth.service';
import { environment } from '../environments/environment';
Think of the 'modules' as the bit after "from". The modules contain the symbols to be imported.
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
It doesn't matter what the symbols are; it is only important that import lines are alphabetised by the module. e.g.
In the Angular codebase it is evident that lowercase characters are listed after capitalised or uppercase characters, hence: 'rxjs/Rx' is listed before 'rxjs/add/observable/of' and 'rxjs/add/operator/catch'
import { TestBed, async, inject } from '@angular/core/testing';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/of';
Think of a "destructured imported symbol" as an exported symbol that has been extracted from its parent module. In this example AngularFireAuth, Observable, TestBed, async and inject are all destructured imported symbols.
Again, the order is alphabetised; uppercase first, then capitalised and then lowercase. Here is a great example from the Angular codebase:
import {CUSTOM_ELEMENTS_SCHEMA, Compiler, Component, Directive, Inject, Injectable, Injector, Input, NgModule, Optional, Pipe, SkipSelf, ɵstringify as stringify} from '@angular/core';
There doesn't seem to be any particular order to importing services, pipes, models, directives or components, etc. looking at the Angular codebase
Note, modules in parent directories are imported after those closer to the module they are being imported into e.g.
import { AuthService } from './auth.service';
import { environment } from '../environments/environment';
import { abc } from '.../abc';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With