79126737

Date: 2024-10-25 17:27:54
Score: 0.5
Natty:
Report link

I came to realize that I was looking at my injectors wrong. What I ended up doing is the following:

/*environment.model.ts*/
export interface IEnvironmentModel {
    // ...
}

export const ENVIRONMENT_INJECTION_TOKEN = new InjectionToken<IEnvironmentModel>('environment-model');

/*app.module.ts*/
@NgModule({
    // ...
    providers: [
        {
            provide: ENVIRONMENT_INJECTION_TOKEN,
            useValue: environment
        }
    ]
})
export class AppModule {}

This way, I could do @Injectable({ providedIn: 'root' }) for AuthService as well as all of it's dependencies.

When I tested this, I found that if ENVIRONMENT_INJECTION_TOKEN was not provided in the app module, I would get a null injector error, but as long as it was provided, I could use all of the services that I needed.

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Duncan McPherson