I need help with a service that looks like this:
@Injectable()
export class AuthService {
private contentHeader: Headers = new Headers({"Content-Type": "application/json", "Accept": "application/json"});
constructor(public events: Events, private authHttp: AuthHttp){
}
public get(url): Observable<any> {
return this.authHttp.get(url, { headers: this.contentHeader })
.map(this.extractData)
.catch(this.handleError);
}
public extractData(res: Response) {
try {
let body = res.json();
return body || {};
}
catch (e) {
return {};
}
}
public handleError(error: any) {
if (error.status == 401 || error.status == 403)
{
// injected
this.events.publish('user:logout');
}
return Observable.throw(error);
}
}
The issue is that this.events does not work inside of the handleError function and it triggers this error in the console: EXCEPTION: TypeError: this.events is undefined
Is there a way to use services with dependency injection within the map and catch sections of an Observable?