question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UI-Router doesn't wait for App_Initializer

See original GitHub issue

UI-Router triggers route change as defined in config section before App_Initializer function is executed. In the below example home state’s resolveFn is triggered before App_Initialzer's resolves

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    routingComponents
  ],
  imports: [
    BrowserModule,
    UIRouterModule.forRoot(
      { states: appRoutes, 
        useHash: true ,
        config: uiRouterConfigFn
      }
    ),
  ],
  providers: [
    {
        provide: APP_INITIALIZER,
        useFactory: test,
        multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

function test(): Function {
  return () => delay();
}

function delay(){
  var deferred = new Deferred<any>();
  console.log("Initialization started: " + new Date());
  setTimeout(() => {
    console.log("Initialization Done: " + new Date());
    deferred.resolve();
  }, 5000); // Wait 3s then resolve.
  return deferred.promise;
}

app.routes.ts

export const routingComponents = [
    HomeComponent
  ];


let home = {
    name: 'home',
    url: '/home',
    component: HomeComponent,
    resolve:[
      {
        token: 'te',
        resolveFn:($state: StateService) =>{
          var deferred = new Deferred<any>();
          console.log("testing Home Component");
          deferred.resolve();
          return deferred.promise;
        }
      }
    ]
  }
  
  export const appRoutes = [
    home
  ];
  
  export function uiRouterConfigFn(router: UIRouter, injector: Injector) { 
    // If no URL matches, go to the `hello` state by default
    router.urlService.rules.otherwise({ state: 'home' });
  }

Have attached the full project for reference.

test.zip

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
christopherthielencommented, Feb 4, 2018

As a workaround you can use deferIntercept In the router configuration function and manually call listen and sync after your initializer is done

1reaction
jensbodalcommented, Apr 6, 2018

Can also load the config in main.ts and provide the config.

fetch('/app-config-url').then(data => data.json().then((appConfig) => {
  platformBrowserDynamic(
    [{ provide: 'AppConfig', useValue: appConfig }]
  )
  .bootstrapModule(AppModule)

This is just a simple example using fetch and without typesafety.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Router doesn't wait for APP_INITIALIZER · Issue #340 - GitHub
In its initialization code, the sample hybrid app calls deferIntercept() then calls listen() when angular has bootstrapped. This should happen ...
Read more >
App_Initializer not working with UI-router - Stack Overflow
UI -Router triggers route change as defined in config section before App_Initializer function is executed. In the below example home state's ...
Read more >
APP_INITIALIZER Token in Angular (Advanced, 2022)
In real Angular applications, it might happen that you need to perform some logic or pull the data even before Angular will start...
Read more >
Angular. Router Di Not Working When Using App_Initializer
This tutorial we will what is APP_INITIALIZER is show how to use it in an example app. The Angular injector uses the DI...
Read more >
What's new in Angular v12 - This Dot Labs
It is also a long-waited feature request: in Angular v12, we can now use Observable as APP_INITIALIZER . import { APP_INITIALIZER } from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found