O redirecionamento angular 2 no retorno de chamada de captura http / rxjs causa TypeError: Não é possível ler a propriedade 'subscribe' de undefined
Ao redirecionar a partir da função de captura rxjs, recebo o erro:EXCEPTION: TypeError: Cannot read property 'subscribe' of undefined
Aqui está a 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)
}
}
No componente consumidor:
...
ngOnInit(){
this.getContacts()
}
getContacts(){
this.contactService.getContacts().subscribe(
(contacts) => { this.contacts = contacts },
(error) => { this.errorMessage = error }
)
}
Essa questão (Angular 2. Como lidar com erros 4xx com redirecionamento no Observable?) sugeriria que eu estou fazendo isso direito, mas talvez esteja faltando algo no meu componente?