Angular 4 Injecting route in the APP_INITIALIZER

config-service.ts can be rewritten as below.

@Injectable()
export class ConfigService
    constructor(private injector: Injector){}

    load(): Promise<any> {
        const router = this.injector.get(Router);
        console.log('current url : ' + router.url);
        return new Promise(((resolve, reject) => resolve()));
    }
}

No need to inject Router as a dependency in app.module.ts.


What I was trying to do isn't feasible since the APP_INITIALIZER happens before the router init. https://medium.com/hackernoon/hook-into-angular-initialization-process-add41a6b7e


As another answer pointed out, the APP_INITIALIZER is triggered before the router initializes. In this scenario, you can simply use window.location.pathname, or any other properties under window.location.

Tags:

Angular