Angular Structure
2 min readApr 30, 2022
1: Ideas taken from this project
https://github.com/Ismaestro/angular-example-app/tree/master/src/app
routes.config.ts
import { InjectionToken } from '@angular/core';
export const ROUTES_CONFIG = new InjectionToken('routes.config');
const basePaths = {
customer: 'customer',
//auth: 'auth',
};
const routesNames = {
home: '',
error404: '404',
customer: {
register: 'customer-register',
//detail: ':id',
},
// auth: {
// signUp: 'sign-up',
// logIn: 'log-in',
// },
};
//export const getHeroDetail = (id: string) => `/${basePaths.hero}/${id}`;
export const RoutesConfig: any = {
basePaths,
routesNames,
routes: {
home: `/${routesNames.home}`,
error404: `/${routesNames.error404}`,
customer: {
register: `/${basePaths.customer}/${routesNames.customer.register}`,
//detail: getHeroDetail,
},
// auth: {
// signUp: `/${basePaths.auth}/${routesNames.auth.signUp}`,
// logIn: `/${basePaths.auth}/${routesNames.auth.logIn}`,
// },
},
};
customer.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CustomerRegisterComponent } from './components/customer-register/customer-register.component';
import {CustomerRoutingModule} from "./customer-routing.module";
@NgModule({
declarations: [
CustomerRegisterComponent
],
imports: [
CommonModule, CustomerRoutingModule
]
})
export class CustomerModule { }
customer-routing.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {RoutesConfig} from "../../configs/routes.config";
import {RouterModule, Routes} from "@angular/router";
import {CustomerRegisterComponent} from "./components/customer-register/customer-register.component";
const customerRoutes = RoutesConfig.routesNames.customer;
const customersRoutes: Routes = [
{ path: customerRoutes.register, component: CustomerRegisterComponent }
];
@NgModule({
declarations: [],
imports: [
CommonModule, RouterModule.forChild(customersRoutes)
]
})
export class CustomerRoutingModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {RoutesConfig} from "./configs/routes.config";
import {CustomerRegisterComponent} from "./modules/customer/components/customer-register/customer-register.component";
const routes: Routes = [
// {
// path: RoutesConfig.basePaths.auth,
// loadChildren: () => import('./modules/auth/auth.module').then(m => m.AuthModule),
// },
{
path: 'test',
component: CustomerRegisterComponent
},
{
path: RoutesConfig.basePaths.customer,
loadChildren: () => import('./modules/customer/customer.module').then(m => m.CustomerModule),
},
{ path: '**', redirectTo: RoutesConfig.routes.error404 },
];
@NgModule({
imports: [RouterModule.forRoot(routes,
// {
// initialNavigation : 'enabled',
// scrollPositionRestoration : 'enabled',
// anchorScrolling : 'enabled',
// relativeLinkResolution : 'legacy',
// }
)],
exports: [RouterModule]
})
export class AppRoutingModule { }