title: Angular 2 route How to create router object in service?
body: <p>I have create below rest service but getting error when call this.router.navigate(['/login']); How to create router object in service? this.router.navigate is work from Component class but it's not working from service.</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Http, Headers, Response } from '@angular/http';
  import { Router } from '@angular/router';
  import { Observable }     from 'rxjs/Observable';
  import { apiURL } from './app.config';
  
  @Injectable()
  export class RestService {
      header = new Headers();
  
      constructor(public http: Http, private router: Router) {
  
  
      }
  
      get(url: string) {
          debugger;
          this.addHeader();
          return this.http.get(apiURL + url, { headers: this.header }).catch(this.handleError);;
      }
  
      post(url: string, data) {
          this.addHeader();
          return this.http.post(apiURL + url, data, { headers: this.header }).catch(this.handleError);;
      }
  
      put(url: string, data) {
          this.addHeader();
          return this.http.put(apiURL + url, data, { headers: this.header }).catch(this.handleError);;
      }
  
      auth(url: string, data) {
          this.addHeader();
          return this.http.post(apiURL.replace('api/v1/', '') + '/' + url, data, { headers: this.header }).catch(this.handleError);
      }
  
      private extractData(res: Response) {
          debugger;
          let body = res.json();
          return body.data || {};
      }
  
      private handleError(error: any) {
          // In a real world app, we might use a remote logging infrastructure
          // We'd also dig deeper into the error to get a better message
          debugger;
          if (error.status == '401') {
              this.router.navigate(['/login']);
          }
          else {
              let errMsg = (error.message) ? error.message :
                  error.status ? `${error.status} - ${error.statusText}` : 'Server error';
              console.error(errMsg); // log to console instead
              return Observable.throw(errMsg);
          }
      }
  
      private addHeader() {
          this.header = new Headers();
          this.header.append('Content-Type', 'application/json');
          this.header.append('Authorization', 'Bearer ' + localStorage.getItem('id_token'));
      }
  }
  </code></pre>