Angular 17 – dinamically load an Angular library

  Kiến thức lập trình

I have a requirement to load dynamically a library from an URL (that will know at runtime and has the same origin as the SPA) and “display” a component from that library. The library is an angular one.

What I tried is to build the library and imported

private async loadLibrary(url: string): Promise<NgModuleRef<any>> {
    const module = await import(url);
    const moduleFactory = this.compiler.compileModuleSync(module.default);
    ...

}

The problem that I encountered is that I have the error:
Failed to resolve module specifier “@angular/core”. Relative references must start with either “/”, “./”, or “../”.

I see in my library “./fesm2022/mylibrary.mjs” that I have the imports

import * as i0 from '@angular/core';
import { Injectable, Component } from '@angular/core';

Any ideea would be greatly appreciated.

2

LEAVE A COMMENT