Following a great article on dynamic component loading:
I want to know more about the use of System.import. The author uses a following syntax to load the javascript file of the module to be dynamically loaded.
System.import('../module1.module.js').then((module) => {
this.compiler.compileModuleAndAllComponentsAsync(module.Module1Module)
.then((compiled) => {
const factory = compiled.componentFactories[0];
this.container.createComponent(factory);
});
});
I understand that system.import is used to dynamically load modules at run time.
And in our case, we provide the name of the ts of the module file but because the js file will be also of same name we can provide that in the parameters of import function.
But I was wondering if we could provide a generic JS file in the parameter which not necessarily contains that module but more modules and components also. (These modules can come from other external projects/libraries) And then we can provide one of the module name like following and create components.
System.import('../main-app1.js').then((module) => {
this.compiler.compileModuleAndAllComponentsAsync(module.Module1Module)
.then((compiled) => {
const factory = compiled.componentFactories[0];
this.container.createComponent(factory);
});
});
I have tried above approach but it does not works for me.It fails at run time.
core.js:9110 ERROR Error: Uncaught (in promise): Error: No NgModule metadata found for 'undefined'.
Error: No NgModule metadata found for 'undefined'.