I created and successfully lazily loaded LazyAComponent component.
I didn't have to declare LazyAComponent in the app.module. PERFECT!
However, if I place a component, HelloComponent, on LazyAComponent; I get the error 'hello' is not a known element
I was able to workaround this error by declaring LazyAComponent in the app.module.
However, if I have to declare the component in order to get this work then lazy loading the component has no value whatsoever!?
What am I missing???
EDIT 1: For clarity, this is a part of a much larger project where I am trying to lazy load a component based on the route parameter. Stackblitz does not allow me to illustrate this, however, my real code is loading the component like so:
const template = paramMap.get('template');
const esModule = await import(`/src/app/features/admin/components/article-editor/article-templates/${template}-template/${template}-template.component`);
const componentClassName = 'Eai' + tempalte + 'TemplateComponent';
return esModule[componentClassName];
This is actually working, I had to include the lazy-a folder in the tsconfig, however, as illustrated, it falls apart once I place a custom child component in the template.
EDIT 2: I guess I don't understand the value of lazy and dynamically loading components if you have to manually bootstrap all of it??? Thanks to Nicolas below, I got past my original error, however, after registering my component from the lazy component, the child component could not recognize the "async" pipe.
I'm sure it has uses, but to me w/o this ability, this feature feels very, very useless. I could just be architecting the project wrong?
EDIT 3: OK, thanks everyone. I guess I expected way too much "magic" from Angular. As I understand what I would want/need to do is lazy load each article template and have them share a common set of sub-components (article-blocks) among them.
That being said, we might have up to 50 or more article templates. That 50+ modules. That might be acceptable, however, that also means 50+ route definitions. Is there a way to dynamically lazy load modules based on a route parameter?
Something like:
{
path: 'article/:template',
loadChildren: () => import('./features/article/${template}/${template}.module').then((m) => m[template]),
}
https://stackblitz.com/edit/angular-ivy-pxar92?devtoolsheight=33&file=src/app/app.component.ts
<hello>in your lazy-a template. It's best to remove the gunk that stackblitz starts you off with.