Хочу в сервисе выбрасывать людей на страницу входа. Но выбрасывает ошибку:
Cannot read property 'navigate' of undefined
Подключаю роутер как всегда в конструкторе:
import { Router } from '@angular/router';
constructor(private http: Http,
private authService: AuthenticationService,
private router: Router
) {
this.urlAPI = authService.urlAPI;
}
searchComplaints(reqParms): Observable<any> {
const headers = new Headers;
const options = new RequestOptions({
headers: headers
});
let requestUrl = this.urlAPI + 'complaints/search/?';
for (const parmName of Object.keys(reqParms)) {
if (requestUrl[requestUrl.length - 1] !== '?') {
requestUrl += '&';
}
requestUrl += parmName + '=' + reqParms[parmName];
}
headers.append('Content-Type', 'application/json');
headers.append('Authorization', this.authService.OAuth.token_type + ' ' + this.authService.OAuth.access_token);
return this.http.get(requestUrl, options)
.map(res => res.json())
.catch(this.handleError);
}
private handleError (error: Response | any) {
if(error.status == 0 || error.status == 401){
return this.router.navigate(['/login']);
}
return Observable.throw(error.json().detail || error);
}