0

For example I have a url /videos/5 which shows page 5 of my videos page. How can I use the Angular router to change to page 3 for example. Is there a way to 'relatively navigate' like without having to pass videos to the url.

Something like:

this.router.navigate([`/${pageNumber}`])

(page number is the number of the page I want to go to like /videos/3)

I looked around but can't find a solution I tried

this.router.navigate([`../${pageNumber}`], { relativeTo: this.activatedRoute.parent })

But it's giving me errors.

How to achieve this with Angular router and relative urls.

1 Answer 1

3

Yes, you can use relativeTo with instance of ActivatedRoute, like this:

import { ActivatedRoute, Router } from '@angular/router';

...

constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute
) { }

...

this.router.navigate([`../${pageNumber}`], { relativeTo: this.activatedRoute })
Sign up to request clarification or add additional context in comments.

1 Comment

That works! I was messing around with the point point slash and relative to but didn't try that combination yet. Thank you :)

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.