Using ActivatedRoute in Services
The Challenge
Attempting to utilize ActivatedRoute within a service, I encountered an issue where it was not tracking the current route accurately. It seemed unable to detect any route at all. After spending considerable time troubleshooting, I realized the problem and struggled to find relevant solutions online (perhaps due to my slower pace).
To assist others facing a similar dilemma, I decided to share my experience and solution.
Below is an excerpt of the code that caused me trouble initially.
@Injectable()
export class ImdbService {
constructor(private router: Router,
private route: ActivatedRoute) {
}
closeDetails(): void {
this.detailsOpened = false;
this.router.navigate(['../'], {relativeTo: this.route});
}