immino
immino

Reputation: 68

Angular 9 Dynamically import modules with dynamic routes

I want to import angular modules dynamically with variable string routes from backend service. For example my backend service sends to me this response when app is starting(using APP_INITIALIZER).

{
    "hostname": "a-tenant",
    "modules": {
        "home": {
            "class": "HomeAModule",
            "path": "home-a.module",
         },
    },
},

My app structure is:

 project structure

So i want to import a module like this

const path = `./tenants/${response.hostname}/home/${response.modules.home.path}`;

import(path).then(m => m[response.modules.home.class]);

My final import code should be like this in runtime:

import('./tenants/a-tenant/home/home-a.module').then(m => m.HomeAModule);

But i'm getting this error, i think webpack does not handle dynamic loads like this.

Thank you for your help :)

Error

Upvotes: 3

Views: 3050

Answers (1)

DMITRYTISHKIN
DMITRYTISHKIN

Reputation: 526

Unfortunately, using dynamic string for dynamic imports is impossible

Upvotes: 2

Related Questions