Answer the question
In order to leave comments, you need to log in
Is it possible to dynamically create lazy load routes in Angular 4?
I have 5 different modules with different application themes. Now, each theme has its own prefix in the url, like /firstTheme/page, /secondTheme/page, etc. As you can see, it's not very pretty. Is it possible to somehow dynamically change the address for lazy load to get something like
export const routes = RouterModule.forRoot([
{
path: '',
loadChildren: getPath()
}
])
Answer the question
In order to leave comments, you need to log in
No. I will give an example using Webpack
In the case of JIT compilation, a plugin such as angular-router-loader
.
When the collector is working, it goes through all the routes containing loadChildren
and replaces them with some expression:
{
loadChildren: () => new Promise(resolve => {
(require as any).ensure([], require => {
resolve(require('./path/to/yourmodule').ModuleName);
})
})
}
@ngtools/webpack
, which does a similar job, while it also compiles all components into *.ngfactory.ts files. After that, webpack also generates chunks with these factories. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question