lmeijdam/angular-umd-dynamic-example

Import module in dynamic loaded modules

Closed this issue · 11 comments

Lets say i have DynamicModuleA and DynamicModuleB that are my modules to load on runtime.
I have another module SharedModule with some shared components and i want to use this in both DynamicModuleA and DynamicModuleB. How can I do this?

Hi. You just need to build SharedModule as a library & import it in AppModule, DynamicModuleA and DynamicModuleB.

If you have some providers in SharedModule (that's not recommended but possible, you can use SharedModule.forRoot() in AppModule & SharedModule in feature modules.

Clear enough?

This is what i was doing but i was getting an error when i was dynamically loading the moduleA and moduleB. I will try again with fresh projects. Thanks for the reply

well i tried again what you said but i am getting this error in console when i load my dynamic module:
TypeError: Cannot read property 'ɵmod' of undefined

I used successfully the shared library in my app.component which is not dynamically loaded.
In my dynamic modules i used successfully a 3rd party library (the ng-select).
My only problem is with my own libraries...

Do you have any idea? thank again

Can I see a sample of code ?

Your library is built before the run ? I mean you can't import directly from your source, it needs to be transfert to node_modules. You have two solutions, build & export to a folder like node_modules or export to npm (a bit "overkill").

I am building my library and then pack it with npm pack and then install it using npm install ...filePath.tgz

Everything is local right now but during the day I will manage to upload sample code on github. I will let you know

And what's your angular version ?

9.0.6 both library and app

Hum, I didn't try with angular 9, it may contains breaking changes... Like ivy for exemple ? Try disable it. I don't now, I'll try it as soon as I have time. Good luck :)

ok so have you already done what i am trying to do but with different version? and works? which version? thanks again

Yes it works with Angular 7 (7.2.7)

got it working with angular 9. Ivy must be disabled as you suggested. Thanks again!