title: Angular 4 load data before initialize the application
body: <p>I'm trying to load some data before my application starts. The reason why I need to do it, is because some of the menu has restricted access based on some user condition, for example, based on the user location.</p>
  
  <p>So if the user doesn't have a location yet or it's location isn't enabled to access certain view, I need to block it.</p>
  
  <hr />
  
  <p>I tried to use Angular Resolve method, without success, this is what I did:</p>
  
  <p><strong>Resolve guard</strong></p>
  
  <pre><code>// The apiService is my own service to make http calls to the server.
  @Injectable()
  export class AppResolveGuard implements Resolve&lt;any&gt; {
      constructor(
          private _api: ApiService,
          private _store: Store&lt;IStoreInterface&gt;,
      ) { }
  
      resolve(): any {
          return this._api.apiGet('loadData').subscribe(response =&gt; {
              this._store.dispatch({
                  type: USER_FETCH_DATA,
                  payload: response.usuario
              });
  
              return response;
          });
      }
  }
  </code></pre>
  
  <p><strong>Routing</strong></p>
  
  <pre><code>export const routing = [
      {
          path: '', component: AppComponent, canActivateChild: [AuthGuard], resolve: {app: AppResolveGuard},
          children: [
              { path: 'home', component: HomeComponent },
              { path: 'company', canActivate: [LocationGuard], component: CompanyComponent },
          ]
      }
  ];
  </code></pre>
  
  <blockquote>
    <p>The AuthGuard is just going to check for a valid cookie session.</p>
  </blockquote>
  
  <p>As you can see, the routing I'm trying to block access is the <code>company</code> routing.
  I'm able to prevent access when I first load another page, for example, if the first access is to the page <code>home</code> and then I navigate to the page <code>company</code> the validation works fine, because the user data and location is already avaliable.</p>
  
  <p>However, if the first access is to the page <code>company</code>, it will block all users to navigate there, because at the time the <code>LocationGuard</code> try to validate the user location, the data isn't avaliable yet.</p>
  
  <p>I also tried to replace <code>Resolve</code> with <code>CanActivate</code>, but it never enables the view after the data is avaliable, it stays on a blank page.</p>