Load all parent routes dynamically in angular when app is bootstraped or loaded

305 views Asked by At

I am using angular 2 to build an application where I need to check the base href and match it with my predefined sets of parent routes in my app.routes.ts file.

Currently I have created a hard-coded array of parentRoutes = ['login', 'signup', 'admin', 'user', 'manage'] in the provider which is used in the following way:

export let customProvider: any = {
    provide: APP_BASE_HREF,
    useFactory: () => {
        let tenant = window['_app_base'];
        let parentRoutes = ['login', 'signup', 'admin', 'user', 'manage'];
        if (tenant !== '/') {
            if (parentRoutes.indexOf(tenant) > -1)
                window['_app_base'] = '/notenant';
            else
                window['_app_base'] = tenant;
        }
        else
            window['_app_base'] = '/default'; // get default tenant from API

        return window['_app_base'];
    }
}

I need to load this parentRoutes array dynamically using the following service:

for (let i = 0; i < this.router.config.length; i++) {
    this.parentRoutes .push(this.router.config[i].path);
 }

But this has to be done in the customProvider (written above) inside app.module.ts

I tried calling a service but it doesn't seem possible from the app.module.ts file. Additionally there is an lifecycle issue. At the time the app is loaded, the customProvider check in app.module.ts is ran before this.routePath is defined.

0

There are 0 answers