La redirección angular 2 en la devolución de llamada http / rxjs provoca TypeError: no se puede leer la propiedad 'suscribirse' de undefined
Al redirigir desde la función de captura rxjs me sale el error:EXCEPTION: TypeError: Cannot read property 'subscribe' of undefined
Aquí está la parte relevante decontactService
:
getContacts () {
...
return this.http.get(this.apiUrl, options)
.map(this.extractData, this)
.catch(err => {
this.handleError(err)
})
}
...
handleError (error) {
if(error.status === 401){
//the redirect works great, except, when added, I get the exception error
this.router.navigate(['/login'])
return Observable.of([])//this is my attempt based on link below
}
else{
// In a real world app, we might send the error to remote logging infrastructure
let errMsg = error.message || 'Server error'
console.error(errMsg) // log to console instead
return Observable.throw(errMsg)
}
}
En el componente consumidor:
...
ngOnInit(){
this.getContacts()
}
getContacts(){
this.contactService.getContacts().subscribe(
(contacts) => { this.contacts = contacts },
(error) => { this.errorMessage = error }
)
}
Esta pregunta (Angular 2. ¿Cómo manejar los errores 4xx con redireccionamiento en Observable?) sugeriría que estoy haciendo esto bien, pero ¿tal vez me falta algo en mi componente?