Angular 4 Injecting route in the APP_INITIALIZER

前端 未结 2 1018
深忆病人
深忆病人 2021-02-14 16:52

I am trying to retrieve a data present in the url in my APP_INITIALIZER

app.module.ts

export function init(config: ConfigService, router: Router) {
    r         


        
相关标签:
2条回答
  • 2021-02-14 17:34

    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.

    0 讨论(0)
  • 2021-02-14 17:39

    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

    0 讨论(0)
提交回复
热议问题