4

I am trying to make my app concious of dev and prod mode and am using the built in mechanism in the Angular CLI, which substitutes the environment.ts file depending on the currently used alias. This works fine for me for switching conts values (like my firebase app key, and logging behaviour)..

Now I'd like to do a little bit more logic in a module.ts file - more specifically I'd like to register different routes depending on the mode (why: I want to include new features only on dev, and I want to remove some route guards in dev mode).

Naively I tried the following (in foo.module.ts):

import { environment } from 'environments/environment';

const fooRoutes: Routes = [
    { path: 'foo', component: FooComponent, canActivate: [fooGuard]  }
];
const devFooRoutes: Routes = [
    { path: 'foo', component: FooComponent},
    { path: 'bar', component: BarComponent}
];

let routes: Routes;
if (environment.production) {
    routes = fooRoutes;
} else {
    routes = devFooRoutes;
}

@NgModule({
    imports: [
        RouterModule.forChild(routes)
    ],
    exports: [
        RouterModule
    ]
})
export class FooRoutingModule { }

but this does not work - the compiler does not like runtime-evalauted code outside of the class. So my question is: How do I include logic in a module file? Is there a better way to accomplish this?

P.S.: Before you suggest to define the Routes const in the environment.ts file, let me say:

  • I want to be able to register the needed routes for a feature in the feature module and not globally in the environment.ts
  • More severely: Even if I do that I will end up with circular dependencies because I would need to import the components I address in the routes in the environment.ts file, which is itself a dependency of the feature module.
EluciusFTW
  • 2,565
  • 6
  • 42
  • 59

0 Answers0