Currently, I am encountering a challenge with TypeScript and Angular 2. The structure of my TS class is as follows:
'import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-blogdetail',
templateUrl: './blogdetail.component.html',
styleUrls: ['./blogdetail.component.css']
})
export class BlogdetailComponent implements OnInit {
blogId:any;
title:any;
constructor(private _activatedRoute:ActivatedRoute) {
// Does not work
// this.blogId = this._activatedRoute.snapshot.params['id'];
//this.title= `This is blog detail for blogID:${this.blogId}`;
}
ngOnInit() {
this.blogId = this._activatedRoute.snapshot.params['id'];
this.title= `This is blog detail for blogID:${this.blogId}`;
}
}'
I am trying to retrieve the route parameter in the ngOnit event. When I initially attempt to obtain the parameter in the TS class constructor, the value of the blogId variable is returned as undefined. Upon analyzing the sequence of events, it seems to resemble the flow illustrated in the image below: ![flow]https://i.stack.imgur.com/41fpe.png
Is it mandatory to always retrieve the activatedRoute snapshot values in ngOnIt?