In the earlier versions of Angular CLI, specifically those before 1.5.0, it was common practice to import all RxJs operators and statics into a single file for easy usage throughout the application.
For example:
rxjs-operators.ts
// Statics
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/range';
import 'rxjs/add/observable/concat';
import 'rxjs/add/observable/merge';
import 'rxjs/add/observable/empty';
// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';
...
app.module.ts
// OPERATORS
import './rxjs-operators';
@NgModule({
declarations: [
AppComponent
],
imports: [...],
providers: [...],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
],
bootstrap: [AppComponent]
})
export class AppModule { }
Nowadays, with the introduction of the RxJs
pipe
operator, it is recommended to import these operators directly within the specific module in which they are used.
For instance:
...
import { Observable } from 'rxjs/Observable';
import 'rxjs/util/pipe';
import { take, catchError } from 'rxjs/operators';
@Injectable()
export class AccountDetailsResolver implements Resolve<AccountDetails> {
constructor(private membersApi: MembersApiService,
private router: Router,
private navigationManager: NavigationManagerService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<AccountDetails> {
if(route.paramMap.get('accountId')) {
return this.membersApi.getBasicDetails(route.paramMap.get('accountId')).pipe(
catchError(error => {
this.navigationManager.navigateToDefaultScreen();
return Observable.of(null);
}),
take(1)
);
} else {
return Observable.of(null);
}
}
}
The question arises whether there is still a way to centralize the import of all operators and static methods or if it's now required to include the import statements individually in each Angular definition such as
Module
, Component
, Service
, Pipe
, Directive
, etc.