Route segments have been moved to constant. The code below is simplified, but in project about 5 lazy modules, each have about 20 routes, and segments often used in services and components for router.navigate
and as string keys in objects (f.e. image for some components: assets/images/${urlSegment}.jpg
).
For this reason segments have been moved to constants.
routing.module.ts
export const PATIENT_URL_SEGMENTS = {
TOOLS: 'tools',
INDEX: 'index',
};
const routes = [
{ path: PATIENT_URL_SEGMENTS.TOOLS, component: ToolsComponent }
{ path: `${PATIENT_URL_SEGMENTS.TOOLS}/:${PATIENT_URL_SEGMENTS.INDEX}`, component: ToolDetailsComponent }]
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: []
})
export class PatientRoutingModule { }
RoutingModule
is used in lazy loaded module. If I import URL_SEGMENTS
from a separate file, in browser console I see the error:
Can not read split of undefined
But if a constant is declared in place as in the example above, all is ok.
There is one small problem, circular dependency warnings:
One warning for each component that imports PATIENT_URL_SEGMENTS
, because at the same time these components have been imported in the routing module. In fact there is no error, but I'd like to remove warnings for cases when components import PATIENT_URL_SEGMENTS
.
Do you know how to achieve this? Maybe it is possible to move constant declarations to a separate file?