Angular Flashcards
order of import statements for Routing modules to work should be considered carefully
angular registers all the routes in the order of the import statements, if there is wild card or '' in the initial module all the routings for the modules that are declared later will not work
All the feature routing modules should be imported before the AppRoutingModules
To fix this all we have to do is, change the import order of the modules in the AppModule. Import EmployeeModule before AppRoutingModule as shown below.
@NgModule({ declarations: [ AppComponent, HomeComponent, PageNotFoundComponent ], imports: [ BrowserModule, EmployeeModule, AppRoutingModule, HttpClientModule, ], providers: [EmployeeService], bootstrap: [AppComponent] }) export class AppModule { }
have page not found component template for wild card route path’**’
const appRoutes: Routes = [
{ path: ‘home’, component: HomeComponent },
{ path: ‘’, redirectTo: ‘/home’, pathMatch: ‘full’ },
{ path: ‘**’, component: PageNotFoundComponent }
];
core module services should be singleton, so the module needs to be imported only once in the app-module
core module services should be singleton, so the module needs to be imported only once in the app-module
browserModule should be imported only once in the app-module
browserModule should be imported only once in the app-module
use forchild for routes in the features modules and forRoot only once in the app-module
use forchild for routes in the features modules and forRoot only once in the app-module
Shared modules should not have providers
shared modules should not import or re-export Modules have providers
lazy loaded modules creates their own branch on the dependency injection tree
if a lazy loaded module imports the shared module, we endup with more than one instance of service provided by the shared module
The SharedModule may re-export other common angular modules, such as CommonModule, FormsModule, ReactiveFormsModule etc. Instead of writing the same code in every feature module to import these commonly used Angular modules we can re-export them from a SharedModule, so these commonly used Angular Modules are available to all the feature modules that import this SharedModule.
The SharedModule may re-export other common angular modules, such as CommonModule, FormsModule, ReactiveFormsModule etc. Instead of writing the same code in every feature module to import these commonly used Angular modules we can re-export them from a SharedModule, so these commonly used Angular Modules are available to all the feature modules that import this SharedModule.
The SharedModule is then imported by all the FeatureModules where we need the shared functionality. The SharedModule can be imported by both - eager loaded FeatureModules as well as lazy loaded FeatureModules. We will discuss eager and lazy loading modules in our upcoming videos.
The SharedModule is then imported by all the FeatureModules where we need the shared functionality. The SharedModule can be imported by both - eager loaded FeatureModules as well as lazy loaded FeatureModules. We will discuss eager and lazy loading modules in our upcoming videos.
By default all the components, directives and pipes in a module are available only to that module; we need to have a exports array to include in other modules
By default all the components, directives and pipes in a module are available only to that module; we need to have a exports array to include in other modules
if we want to share any components, directives or pipes from our shared module
we need to include them in declartions and export arrays
this makes other feature modules to use these components that are shared
if we want to share any components, directives or pipes from our shared module
we need to include them in declartions and export arrays
this makes other feature modules to use these components that are shared
we could export module with without including in the imports module in the shared modules all the modules that import the shared modules could use anything in this module like reactiveFormsModule, CommonModule
we need not reimport the formsmodule or commonModule
import { NgModule } from ‘@angular/core’;
import { CommonModule } from ‘@angular/common’;
import { ReactiveFormsModule } from ‘@angular/forms’;
@NgModule({
imports: [
// At the moment we do not have any components in this SharedModule
// that require directives of CommonModule or ReactiveFormsModule
// So we did not include them here in the imports array. We can
// export a module without importing it first
],
declarations: [],
// Our Employee FeatureModule requires the CommonModule directives
// such as ngIf, ngFor etc. Similarly, Employee FeatureModule also
// requires ReactiveFormsModule directives. So export CommonModule
// and ReactiveFormsModule.
exports: [
CommonModule,
ReactiveFormsModule
]
})
export class SharedModule { }
Importing in feature module:
In EmployeeModule (employee.module.ts), remove CommonModule and ReactiveFormsModule references as these modules are now provided by the imported SharedModule.
import { NgModule } from ‘@angular/core’;
import { EmployeeRoutingModule } from ‘./employee-routing.module’;
import { CreateEmployeeComponent } from ‘./create-employee.component’;
import { ListEmployeesComponent } from ‘./list-employees.component’;
import { SharedModule } from ‘../shared/shared.module’;
@NgModule({ imports: [ EmployeeRoutingModule, SharedModule, ], declarations: [ CreateEmployeeComponent, ListEmployeesComponent ] })
export class EmployeeModule { }
Grouping routes and creating component less route in angular
All the routes in an angular module that you want to lazy load should have the same route prefix.
we could add prefix by path and defining children property;
const appRoutes: Routes = [
{
path: ‘employees’,
children: [
{ path: ‘’, component: ListEmployeesComponent },
{ path: ‘create’, component: CreateEmployeeComponent },
{ path: ‘edit/:id’, component: CreateEmployeeComponent },
]
}
];
Code explanation :
Notice we have a parent route with path employees
The parent ‘employees’ route has 3 child routes
All the 3 child routes will be pre-fixed with the parent route path - employees
Notice the parent route(employees) does not have a component associated with it. That is why this route is called a component less route.
With the above route configuration, we have the following routes
Route Path Description
/employees Displays the list of all employees
/employees/create Allows to create a new employee
/employees/edit/1 Allows to create a edit an existing employee
angular route paths are case sensitive
angular route paths are case sensitive
{ path: ‘create’, component: CreateEmployeeComponent },
all the angular modules are lazy loaded by default
all the angular modules are lazy loaded by default
How to Lazy load
Network tab:
11 request
4.3 MB of data transfer/downlaoded
load time 3.33s
when we navigate to feature module:
we had only one request for getting the data, as all the feature modules components, directives, pipes are already downloaded when we bootstrapped the application
Unless, you are using lazy loading, all the modules are eagerly loaded. This means, all the modules in your angular application and their associated components, directives, pipes and services must be downloaded from the server, when the user first visits the application. Depending on the number of modules in your application and the internet speed, this could take a significant amount of time and may very badly affect the end user experience.
To address this problem, we use asynchronous routing, which loads feature modules lazily, on demand. This can significantly reduce the initial load time of your application.