Titus Sutio Fanpula
Titus Sutio Fanpula

Reputation: 3613

How To Import Module In Angular Using Path In A Variable

I want to import my angular module using variable name as a path of that module. For an example:

When I use this code below, it's working fine:

import('src/app/modules/public/public.module').then(mod => mod.PublicModule);

But, when I try to store my module path to the some variable, It's not working. For an example, what I was trying before, It's looks like this code below:

const publicPath = 'src/app/modules/public/public.module';
import(publicPath).then(mod => mod.PublicModule);

When I trying that code above, I get some error like this:

core.js:6185 ERROR Error: Uncaught (in promise): Error: Cannot find module 'app/modules/public/public.module'
Error: Cannot find module 'app/modules/public/public.module'

Now, What should I have to do to import that module with path in a variable?

Thanks in advance.

Upvotes: 2

Views: 775

Answers (1)

HTN
HTN

Reputation: 3604

You can't. In order to get lazy loading working, Angular CLI analyzes the code to create a separate public.module bundle with this code:

import('src/app/modules/public/public.module').then(mod => mod.PublicModule);

and loads the bundle when needed at runtime. When using a variable, it can not create a bundle anymore as the value is only known at runtime.

Upvotes: 1

Related Questions