2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.