I'm using standalone components in my Angular 15 app, and I'm realizing that 4-5 of them are going to be useful across projects, so I considered moving them into an npm library. Everything I know about libraries though is that they're done via modules. Is there something special that needs to be done for standalone, or is it just "You don't use standalone in a library"?
I tried to just put the standalone components there without any module file, and then I exported the components. It compiles file, but when I run I get a ton of errors saying:
G0203: 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
.
Those errors don't exist before pulling the components out.
In my library component I've done this:
export const PERSON_DEFAULT_OPTIONS = new InjectionToken<IPersonDefaults>('Graph Toolkit person defaults')
export interface IPersonDefault { ... }
@Component({...})
export class PersonComponent {
readonly #config = inject(PERSON_DEFAULT_OPTIONS, {optional: true})
}
That seems to be what's triggering the error. I also tried the "old" syntax:
constructor(@Optional() @Inject(PERSON_DEFAULT_OPTIONS) ...
The app that calls the library provides that token in main.ts