title: Angular 2 - Routing in service
body: <p>I created a service to check for user token.</p>
  
  <p>If the token hasn't expired, do nothing. if it's expired - redirect him to the login page.</p>
  
  <p><strong>My Service.ts relevant code:</strong></p>
  
  <pre><code>checkToken(myError) {
          if (myError.json().error == "token_expired")
          {
              console.log("Your token has expired.");
              this.router.navigate( ['Login'] ); //Returns error
          } else {
              console.log("Do nothing");
          }
      }
  </code></pre>
  
  <p><strong>The problem</strong></p>
  
  <pre><code>EXCEPTION: TypeError: Cannot read property 'navigate' of undefined
  </code></pre>
  
  <p>My Service.ts</p>
  
  <pre><code>    import {Component,Injectable} from "angular2/core";
  import {Router} from 'angular2/router';
  
  
  @Injectable()
  
  export class JwtService {
      private jwtToken = localStorage.getItem("jwt");
  
      constructor(public router: Router) 
  
      getToken() {
          return this.jwtToken;
      }
  
      checkToken(myError) {
          if (myError.json().error == "token_expired")
          {
              console.log("Your token has expired.");
              this.router.navigate( ['Login'] );
          } else {
              console.log("Do nothing");
          }
      }
  }
  </code></pre>
  
  <p>My <code>ROUTER_PROVIDERS</code> is in my <code>boot.ts</code> file:</p>
  
  <pre><code>bootstrap(AppComponent, [
    JwtService,
    HTTP_PROVIDERS,
    ROUTER_PROVIDERS
  ]);
  </code></pre>
  
  <p>then why I can't navigate from my service?</p>