3

Example used is given in this stackblitz

This is how routing is structured

    RouterModule.forRoot([
      {
        path: 'login',
        component: LoginViewComponent,
        children: [
          { path: 'home', component: HomeViewComponent },
          { path: 'catalog/:id', component: CatalogViewComponent }
        ]
      },
      { path: '**', redirectTo: 'login' }
    ])
  ],

When I am in the login component, I can go to home component with this absolute route

this.navigate(['login/home']);

I can also use navigateByUrl() like this

this.navigateByUrl(['login/home']);

But I don't understand how to route relatively with navigate()

this.navigate(['home'], { relativeTo: this.route });
this.navigate(['/home'], { relativeTo: this.route });

None of these methods are routing to homeComponent

0

1 Answer 1

2

Instead of making child routes, I just have another route with the same name in the path:

app-routing.module.ts

{ path: "activity", component: ActivityComponent },
{ path: "activity/orders", component: OrdersComponent },

If I am in the activity component, I navigate to orders by:

this.router.navigate(["orders"], { relativeTo: this.route });`

import and constructor in the activity component

import { Router, ActivatedRoute } from "@angular/router";
constructor(
  private router: Router,
  private route: ActivatedRoute,
) {
Sign up to request clarification or add additional context in comments.

1 Comment

I created this project to understand how relative routing works in Angular. Your answer is more like a workaround and does not explain why it doesn't work in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.