Angular-cli: module logical file depending on environment

I'm trying to make my application interesting for dev and prod mode, and I use the built-in mechanism in the Angular CLI, which replaces the environment.ts file depending on the alias currently in use. This is great for me for switching conts values ​​(like my firebase application key and logging).

Now I would like to do a little more logic in the module.ts file - more precisely, I would like to register different routes depending on the mode (why: I want to enable new functions only on dev, and I want to remove some route protection devices in dev mode) .

I naively 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 { }

- . : ? ?

P.S.: const environment.ts, :

  • , .ts
  • : , , , environment.ts, .
+4

Source: https://habr.com/ru/post/1691326/


All Articles