Routing in angular
Routes
Route provides information about which component maps to a specific path.
{ path:’login’,component:LoginComponent}
For example, this is the definition of route that maps ‘login’ path to the ‘LoginComponent’ component.
Steps for perform routing in Angular
- Import RouterModule in app-routing.module.ts from ‘@angular/router’
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './components/login/login.component';
const routes: Routes = [
{ path:'login',component:LoginComponent},
{ path:'',redirectTo:'/login',pathMatch:'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
2. Add the router outlet in app.component.html
<router-outlet></router-outlet>
3. Then import AppRoutingModule in app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './components/login/login.component';
@NgModule({
declarations: [
AppComponent,
LoginComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }