Search code examples
angularangular6angular-routing

To route back previous component after routing with clicked object


I have three components header , list & home

In header component routing path for list component will be given like this:

  <div>
    <a [routerLink]="['../list']" >List</a>
  </div>

This header component is placed inside both list and home components.

In list component i am displaying some data in cards like this:

enter image description here

Now when the user click on the particular card,user will routed to home component along with the clicked object id like this:

enter image description here

components code

home.html

<app-header></app-header>
<h2>List Component</h2>
<div *ngFor="let contact of contacts" >

 <mat-card [routerLink]="['../home' , contact.id]"  >
    <h4>{{contact.contactName}}</h4>
     <p>{{contact.email}}</p>
</mat-card>
</div>

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router'; 
import { ListComponent } from './list/list.component';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
    { path: '', component: ListComponent },
    { path: 'list', component: ListComponent },  
    { path: 'home/:id', component: HomeComponent },  
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule { }

home.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  public id: string;
   public sub: any = {};
  constructor(public route: ActivatedRoute) { }

  ngOnInit() {
     this.sub = this.route.params.subscribe(params => {
        this.id = params['id'];
        });
        console.log(this.id);
  }

}

home.html

<app-header></app-header>
<h2>Home Component</h2>
Clicked ID ==> {{id}}

Now the issue is:

  • When i click on home components the url will be https://XXXX-XXX-XX/home/02
  • Now from home component if i click the list (present in header component), then url is changing https://XXXX-XXX-XX/home/list

How can i route back to list component from home component?

Stackblitz demo


Solution

  • By adding:

    { path: 'list', component: ListComponent }
    

    So your Routes will look like:

    const routes: Routes = [
      { path: '', component: ListComponent }, -- for default page
      { path: 'list', component: ListComponent },  
      { path: 'home/:id', component: HomeComponent },  
    ];
    

    and HTML Code:

    <div>
        <a [routerLink]="['/list']">List</a>
    </div>
    

    The problem with the current code:

    You are using ../list in which .. means one path level up (so if you're on /home/home.component.html and you entered ../list.component.html then it refers to the folder of home and then search for list.component.html file)

    Forked_StackBlitz