Adequate Infosoft
Routing allows the user to create a single-page application with multiple views and allows navigation between them. We can switch to any view without changing its state or properties.
Steps to Route:
Create an >Angular App
Create two or more components to route using the following command:
Write the following code in the app.component.html file
<h1>Routing tutorial in Angular</h1>
<a routerLink="">Home</a>
<br>
<a routerLink="about">About</a>
<br>
<a routerLink="user">User</a>
<router-outlet> </router-outlet>
Here, the router-outlet is routing functionality that is used by the router to mark where in a template a matched component should be inserted.
Then, inside the app-routing.module.ts file, we have provided these routes and let angular know about them.
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
import { UserComponent } from './user/user.component';
const routes: Routes = [
{
path : 'about',
component : AboutComponent
},
{
path : 'user',
component : UserComponent
},
{
path : '',
component : HomeComponent
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Add then simply import "AppRouting" module inside the app/module.ts file inside the @NgModule imports.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AngularPipeComponent } from './angular-pipe/angular-pipe.component';
import { UserComponent } from './user/user.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
@NgModule({
declarations: [
AppComponent,
AngularPipeComponent,
UserComponent,
HomeComponent,
AboutComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Now, run the command using ng serve in CLI.
The output will be shown like this :
Click on the link and you will route to that view without loading a page.
Suggested blogs:
>Create a One Page App with Angular.js, Node.js and MongoDB
>How to make the dropdown for this button ADA accessible in Angular?
>How to update properties value with event in template Angular?
>Unit Test Angular Standalone Component, Overriding Provider not used
>How to Import componet's own module using Angular?
>Why component does not display variable of another component in Angular?
>What are the ways to pass parameters to Angular service?