I am working on an Angular 7 component where I extract a route parameter value:
export class PostComponent implements OnInit {
postId: number;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
console.log(this.postId);
this.route.paramMap.subscribe(parameters => {
this.postId = +parameters.get('postId');
})
console.log(this.postId);
}
}
In the initial Console Log, postId
is undefined as expected.
In the subsequent Console Log, postId
reads as 0 since it's not present in the URL.
I aim to maintain postId
as undefined if it's absent from the URL. How can this be achieved?