Wednesday, 28 November 2018

Angular6 Routing


  • To generate new component employee. Go to app root in CLI and run below command
    • ng g c employee/create-employee --spec=false --flat=true ng g c employee/list-employees --spec=false --flat=true
  • component.ts, html and css files will be genarated in employee folder in app and it will add List Employee and Create Employee components in app.module.ts as below
    • app.module.ts
    • import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { CreateEmployeeComponent } from './employee/create-employee.component'; import { ListEmployeesComponent } from './employee/list-employees.component'; @NgModule({ declarations: [ AppComponent, CreateEmployeeComponent, ListEmployeesComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
  • Generate routing module to app. It will create app-routing.module.ts file and add Routing  to app.module.ts
    • ng g m app-routing --flat=true --module=app
  • Inside app-routing.module.ts, add  RouterModule object and call forRoot Method as below.
    • import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { ListEmployeesComponent } from './employee/list-employees.component'; import { CreateEmployeeComponent } from './employee/create-employee.component'; const routes: Routes = [ {path: 'list', component: ListEmployeesComponent}, {path: 'create', component: CreateEmployeeComponent} ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
  • Go to app.component.html and add new routes using routerLink tag

No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *