How to get Route Path Parameters in an Angular service

John Doe
1 min readJan 9, 2020

--

After searching the web for a simple to use solution that will work in Angular8+, I found out people can’t get their head around the problem so i wrote this simple solution which will provide you a single source of truth for the latest router params values:

@Injectable({
providedIn: 'root'
})
export class MyParamsAwareService {
constructor
(private router: Router) {
this.router.events
.pipe(
filter(e => (e instanceof ActivationEnd) && (Object.keys(e.snapshot.params).length > 0)),
map(e => e instanceof ActivationEnd ? e.snapshot.params : {})
)
.subscribe(params => {
console.log(params);
// Do whatever you want here!!!!
});
}
}

--

--