how to navigate to 404 page by using Injectable service in angular2

330 views Asked by At

this is my service :

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { Router} from '@angular/router';
@Injectable()

export class LoginService {
constructor(private http: Http, public router: Router){}

  login1( loginParams: any ){
    let params = new URLSearchParams();
    params.set('login', loginParams.login);
    params.set('password', loginParams.password);
    let loginUrl = 'api/admins/login';
    return this.http
                .post(loginUrl, params)
                .map(this.extracData)
                .catch(this.handleError);
  }
....
 private extracData( res: Response ){
    // console.log(res.json());
    let body = res.json();
    console.log(body.statusCode);
    if (body.statusCode == 404) {
      this.router.navigate(['/error', 404]);
      // return false;
    }
    return body || {};
  }

  private handleError( error: any ){
    let errMsg = (error.message)?error.message:
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.log(error.status);
        return Observable.throw(errMsg);
  }

}

then ,when the body.statusCode is 404 ,it can't navigate 404 page,but process handleError function, so ,i dont't know why angular2 dont't process navigate function ?

and that's my error routes

{
    path: 'error/:id',
    component: errorComponent
  }
0

There are 0 answers