I have authored a library for extracting environment variables that are set from server-side rendering.
This library provides a base class to extend. getEnvironmentValues()
has been omitted for brevity:
@Directive()
export class NgxEnvironmentService<T> {
environment: T;
constructor(
@Inject(ENVIRONMENT_CONFIG)
private readonly environmentConfig: IEnvironmentConfig,
@Inject(PLATFORM_ID)
private readonly platformId: string,
) {
if (isPlatformBrowser(this.platformId)) {
this.environment = this.getEnvironmentValues<T>();
}
}
}
Here is the ENVIRONMENT_CONFIG
token:
import { InjectionToken } from '@angular/core';
import { DEFAULT_CONFIG } from './constants';
import { IEnvironmentConfig } from '../interfaces';
export const ENVIRONMENT_CONFIG = new InjectionToken<IEnvironmentConfig>('environment-config', {
factory: (): IEnvironmentConfig => DEFAULT_CONFIG,
providedIn: 'root'
});
This library is transpiled using ng-packagr. After installation and implementation in the target project:
import { Injectable } from '@angular/core';
import { NgxEnvironmentService } from '@labcorp/ngx-environment';
import { IEnvironment } from '../interfaces';
@Injectable({
providedIn: 'root'
})
export class EnvironmentService extends NgxEnvironmentService<IEnvironment> {}
I receive the following error:
The injectable EnvironmentService inherits its constructor from NgxEnvironmentService, but the latter has a constructor parameter that is not compatible with dependency injection. Either add an explicit constructor to EnvironmentService or change NgxEnvironmentService's constructor to use parameters that are valid for DI.
If I take the source code from the library and copy it into the target project and change the import path, everything works as expected.
This issue: https://stackoverflow.com/questions/60702258/angular-ivy-constructor-is-not-compatible-with-angular-dependency-injection#:~:text=core.js%3A3828%20ERROR%20Error%3A%20This%20constructor%20is%20not%20compatible,of%20this%20class%20is%20missing%20an%20Angular%20decorator is similar, but I'd really like to avoid having to re-implement the constructor in classes that extend NgxEnvironmentService
Has anyone else experienced this error, and how did you fix it?