3

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?

VIK6Galado
  • 650
  • 14
  • 32

3 Answers3

4

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 });
  }
Raghav
  • 1,139
  • 4
  • 15
  • 35
4

You should navigate like this:

this.route.navigate(['home', 'trip-details']);

Or, move trip-details one level higher in the routes, and then you can navigate to it directly with

this.route.navigate(['trip-details']);
ForestG
  • 17,538
  • 14
  • 52
  • 86
2

Because angular can not found the url as ../trip-details

You should use this.route.navigate(['/home/trip-details']);

Juse add 'home' in front of the url

Emon
  • 1,401
  • 14
  • 24