I’m working on an Angular micro frontend setup and running into a dependency injection problem. I have a shared library with a service that needs HttpClient, but when I try to use this service in my micro frontend app, I get injection errors.
Here’s my service in the shared library:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
private settings: any;
constructor(
private httpClient: HttpClient
) {}
async loadAppSettings(): Promise<any> {
try {
const result = await fetch('/app-settings/config.json');
if (!result.ok) {
throw new Error(`Request failed with status: ${result.status}`);
}
this.settings = await result.json();
return this.settings;
} catch (err) {
console.error('Failed to load app settings:', err);
return null;
}
}
}
Then in my micro frontend component:
import { Component } from '@angular/core';
import { DataService } from 'shared-library';
@Component({
selector: 'micro-app',
templateUrl: './micro-app.component.html',
styleUrls: ['./micro-app.component.scss']
})
export class MicroAppComponent {
appTitle = 'micro-frontend';
isMenuCollapsed = false;
constructor(private dataService: DataService) {
this.setupEventListeners();
}
collapseMenu() {
this.isMenuCollapsed = !this.isMenuCollapsed;
}
private setupEventListeners() {
window.addEventListener('localeChange', (evt: any) => {
console.log('Locale changed:', evt.detail);
});
}
}
The error happens as soon as I inject the DataService in the constructor. I’ve tried adding HttpClientModule to imports and using provideHttpClient but the injection error persists. How can I properly set up HttpClient for services in shared libraries used by micro frontends?