title: Passing Value from RouterStateSnapshot to my Root Routing File in Angular 2 App
body: <p>I am trying to figure out how to pass the value from the RouterStateSnapshot in my auth.guard file to my routing file in my Angular 2 app. I want to do this because, rather than loading a hard-coded default component first, I want, after re-login, for the last active component/page to load up. I have this value in my canActivate() function in my AuthGuard file, because I can console out it out via RouterStateSnapshot. So now I need to figure out how to pass this value on to my root routing file so it, on login/re-login, that component gets loaded.</p>
  
  <p>This is the <code>canActivate()</code> function in my <code>AuthGuard</code> file:</p>
  
  <pre><code>canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
  {
      // Get route content id
      let contentId = Object.getPropertyValueAtPath(route, 'data.contentId');
  
      console.log(state.url);
  
      // If route does not have session id, don’t load in tab view
      if (!String.isNotNullOrEmpty(contentId))
      {
          console.error(`Route (${route.routeConfig.path}) does not have a content id.`);
          this.router.navigateByUrl(''); // Forward to default page.
          this.router.navigate([state.url]);
          return false;
      }
  
      if (this.disabled) return true;
  
      if (sessionStorage.getItem('currentUser'))
      {
          // logged in so return true
          return true;
      }
  
      // not logged in so redirect to login page with the return url
      this.router.navigate(['/login', {returnUrl: state.url}]);
      return false;
  }
  </code></pre>
  
  <p>Notice that I am doing this within that function: <code>console.log(state.url)</code>. This gives me the correct value. Now I need to pass it to my app-routing file.</p>
  
  <p>To clarify, currently, on re-login, the last active component <em>is</em> loaded -- but it displays as a background tab, and the default 'redirect' component is what loads up as the active component (i.e, it shows as the active tab).</p>
  
  <p>A simplified version of the app-routing file looks like this:</p>
  
  <pre><code>import { HomeComponent } ...
  
  export const routes: Routes = [
  
  { path: 'login', component: LoginComponent },
  
  { path: 'home', component: HomeComponent },
  
  { path: 'about', component: AboutComponent },
  
  { path: '**', redirectTo: 'home' }
  
  ];
  </code></pre>
  
  <p>As you can see above, on initial load I currently redirect the user to the 'home component' by default. What I'd like to do is re-direct them to the value that is stored in "state.url" from RouterStateSnapshot. I'm not clear how to do this, however. Any ideas as to how I'd pass that value from my AuthGuard file down to my app-routing file? Can I simply inject RouterStateSnapshot into my app-routing file to get that desired value directly? Or can I use "resolve" here along with the path in routing? What's the recommended way to handle this kind of scenario?</p>