Currently, I am working on an Angular 2 application and struggling to grasp the concept of TypeScript's this scope.
Within my TypeScript class named SharedService, there is a function called handleError. If this function encounters a 401 status, I want it to trigger another function called logout(), which is also within the same class.
I have read that in order to successfully utilize functions alongside this, I should define them using arrow function syntax, just like in the example below. However, despite implementing this suggestion, I keep getting the following error:
TypeError: this.logout is not a function(…)
Could anyone provide insights into what mistake I might be making?
export class SharedService {
logout = () => {
console.log('Logout.');
}
//This method catches any errors that might arise upon http requests
handleError(error: any) {
if (error.status === 401) {
this.logout(); <----------------------------- This returns the error
}
console.error(errMsg); // log to console instead
}
}
The issue occurs specifically when attempting to call this.logout().