Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to navigate from sibling component to another sibling component in Angular?

Below is my app.routing.ts

export const Approute: Routes = [
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: 'home',
    component: HomeComponent,
    children: [
      {
        path: 'dashboard',
        component: DashboardComponent
      },
      {
        path: 'trip-details',
        component: TripDetailsComponent
      }
    ]
  },
  { path: 'not-found', component : NotFoundComponent },
  { path: '**', redirectTo: '/not-found' }
 ];

component.ts

 viewDetails(details) {
     ...
    this.route.navigate(['../trip-details']);
  }

Above i have a component and am trying to navigate to another sibling component but am not able to navigate to http://localhost:4200/home/trip-details its redirecting me to http://localhost:4200/not-found.

Where am doing wrong?

like image 800
Rocky Avatar asked Oct 24 '25 18:10

Rocky


1 Answers

You can specify relativeTo which route as shown below:

import {Router, ActivatedRoute } from '@angular/router';
constructor(private r: ActivatedRoute) { }

viewDetails(details) {
    ...
    this.route.navigate(['../trip-details'], { relativeTo: this.r });
  }
like image 178
Raghav Avatar answered Oct 26 '25 09:10

Raghav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!