lmeijdam / angular-umd-dynamic-example

Angular UMD example of loading new modules dynamically
174 stars 57 forks source link

Import module in dynamic loaded modules #24

Closed ahadjithoma closed 4 years ago

ahadjithoma commented 4 years ago

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

LeoLetourneur commented 4 years ago

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?

ahadjithoma commented 4 years ago

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

ahadjithoma commented 4 years ago

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

LeoLetourneur commented 4 years ago

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").

ahadjithoma commented 4 years ago

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

LeoLetourneur commented 4 years ago

And what's your angular version ?

ahadjithoma commented 4 years ago

9.0.6 both library and app

LeoLetourneur commented 4 years ago

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 :)

ahadjithoma commented 4 years ago

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

LeoLetourneur commented 4 years ago

Yes it works with Angular 7 (7.2.7)

ahadjithoma commented 4 years ago

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