I created a simple angular standalone component that uses an injection token for configuration:
export const PERSON_DEFAULT_OPTIONS = new InjectionToken<IPersonDefaults>('...')
export interface IPersonDefaults { ... }
export const providePersonDefaultOptions = (options: IPersonDefaults): Provider => ({
provide: PERSON_DEFAULT_OPTIONS,
useValue: options
})
@Component({...})
export class StandaloneComponent {
readonly #config = inject(PERSON_DEFAULT_OPTIONS, {optional: true})
}
In my main.ts I then just call providePersonDefaultOptions()
with my values and everything works great.
Now I've taken that token/interface/component and put it into an npm library. When I try and run the app again, it gives me the following error:
Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with
EnvironmentInjector#runInContext
I'm not understanding why that code doesn't work as an npm library, since it's still a field initializer where it's being set.