mohitjaiswal28 / Routing-Angular

Angular routing is a powerful feature that allows you to navigate between different components or modules within your Angular application.

Home Page:https://mohitjaiswal28.medium.com/routing-in-angular-0bc54af59caf

Repository from Github https://github.commohitjaiswal28/Routing-AngularRepository from Github https://github.commohitjaiswal28/Routing-Angular

Angular Routing 🌐

Angular routing is a powerful feature that allows you to navigate between different components or modules within your Angular application. It provides a seamless user experience by dynamically updating the content without reloading the entire page.

Path vs Query Parameters

Path vs Query Param

1. Path Parameters πŸ“

1.1 Passing path parameter in routes:

<a [routerLink] = ['book', bookID, 'author', authorID]>
const routes: Routes = [
    {path: 'books/:boodID/author/:authorID', component: BookComponent}
]

1.2 Retrieving path parameter from routes:

There are two ways for retrieving the path param from the routes:

  • snapshot (for static route till the component is active and no change in route)

  • observable (for dynamic route)

  • Using snapshot πŸ“Έ

this.bookIDfromRoutes = this.routes.snapshot.paramMap.get('bookID')
this.bookIDfromRoutes = this.routes.snapshot.params['bookID']
  • Using observable πŸ“‘
this.routes.paramMap.subscribe(params => {
    this.bookIDfromRoutes = params.get('bookID')
})

this.routes.params.subscribe(params => {
    this.bookIDfromRoutes = params['bookID']
})

2. Query Parameters πŸ”

2.1 Passing query parameter in routes:

<a [routerLink] = "['product']" [queryParams]={search: searchValue}>
const routes: Routes = [
    {path: 'product', component: ProductComponent}
]

2.2 Retrieving query parameter from routes:

There are two ways for retrieving the path param from the routes:

  • snapshot (for static route till the component is active and no change in route)

  • observable (for dynamic route)

  • Using snapshot πŸ“Έ

this.product = this.routes.snapshot.queryParamMap.get('search')
this.product = this.routes.snapshot.queryParams['search']
  • Using observable πŸ“‘
this.routes.queryParamMap.subscribe(params => {
    this.product = params.get('search')
})

this.routes.queryParams.subscribe(params => {
    this.product = params['search']
})

3. Lazy Loading πŸ”

Angular only loads modules as needed, rather than loading all modules when the application launches.

How to Clone and Run this Project πŸ–₯️

  1. Clone the repository:
git clone https://github.com/mohitjaiswal28/Routing-Angular.git
  1. Navigate to the project directory:
cd Routing-Angular
  1. Install the dependencies:
npm install
  1. Run the application:
ng serve

About

Angular routing is a powerful feature that allows you to navigate between different components or modules within your Angular application.

https://mohitjaiswal28.medium.com/routing-in-angular-0bc54af59caf


Languages

Language:TypeScript 69.1%Language:HTML 28.8%Language:JavaScript 1.6%Language:CSS 0.6%