What Is the forRoot Method?
The forRoot is a static method and it’s very easy for developers to configure the modules and the best example is - RouterModule.forRoot.
The RouterModule also offers a forChild. It’s also a static method and use to configure the routes of lazy-loaded modules. The forRoot and forChild are the traditional names for methods that configure services in root.
Stayed Informed – Angular4 Documentations with Example
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HttpModule } from '@angular/http'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AppComponent } from './components/app/app.component' import { NavMenuComponent } from './components/navmenu/navmenu.component'; import { HomeComponent } from './components/home/home.component'; import { UserComponent } from './components/user/user.component'; import { UserService } from './components/service/user.service'; import { BarCodePipe } from './components/pipe/custom.pipe'; export const sharedConfig: NgModule = { bootstrap: [ AppComponent ], declarations: [ AppComponent, NavMenuComponent, HomeComponent, UserComponent, BarCodePipe ], imports: [ RouterModule.forRoot([ { path: '', redirectTo: 'home', pathMatch: 'full'}, { path: 'home', component: HomeComponent }, { path: 'user', component: UserComponent }, { path: '**', redirectTo: 'home' } ]) ], providers: [UserService] };
I hope you are enjoying with this post!
Please share with you friends!! Thank you!!!