I'm pretty new to Angular and my question may seem basic but some guidance would be appreciated. I am currently writing an app to teach myself some real development skills. In my app I have an Angular Component that imports a service that I wrote that provides data.
This is my component
@Component({
selector: 'music-instrument-list',
templateUrl: './instrument-report.component.html',
styleUrls: ['./instrument-report.component.css']
})
export class InstrumentReportComponent implements OnInit, OnDestroy {
constructor(public apiService: ApiService) {}
public availableInstruments: any[];
ngOnInit() {
this.apiService.getInstruments().subscribe((result) => {
this.availableInstruments = result;
});
}
ngOnDestroy() {
// how do I unsubscribe?
}
}
This is pretty simple but should I try to add this.apiService.getInstruments.unsubscribe()
to the ngOnDestroy
block I get the error that Property 'unsubscribe' does not exist on type => Observable'. I even considered add .unsubscribe()
after the .subscribe()
like chaining but this just makes my page hang. I get no error either. Can someone please tell me how to best unsubscribe? Do I need to assign the api call to a variable and then use .unsubscribe() on the varable name in the ngOnDestroy
block