I am currently utilizing a Subscription in Angular to extract a parameter from the route. Below is the code snippet:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Subscription} from 'rxjs';
@Component({
selector: 'farm-house',
templateUrl: './house.component.html',
styleUrls: ['./house.component.scss']
})
export class GreenhouseComponent implements OnInit, OnDestroy {
private routeSub: Subscription;
id: string;
constructor(private route: ActivatedRoute) {
this.id = "";
}
ngOnInit(): void {
this.routeSub = this.route.params.subscribe(params => {
this.id = params['id'];
});
}
ngOnDestroy() {
this.routeSub.unsubscribe();
}
}
However, I encountered an issue where the compiler flagged:
Property 'routeSub' has no initializer and is not definitely assigned in the constructor.
Hence, my inquiry revolves around finding the optimal method to initialize a Subscription object?