UI-Router doesn't wait for App_Initializer
See original GitHub issueUI-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.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a workaround you can use
deferIntercept
In the router configuration function and manually calllisten
andsync
after your initializer is doneCan also load the config in
main.ts
and provide the config.This is just a simple example using fetch and without typesafety.