Angular 8:如何在模块中注入ConfigService的getClientID返回值?
Got it, let's tackle this problem. The key thing to remember here is that Angular's module initialization context has specific rules for accessing services—you can't just call a service method directly in the @NgModule decorator's imports array without using Angular's DI tools. Here are two reliable solutions depending on your Angular version:
Solution 1: Use the inject() Function (Angular 14+)
If you're running Angular 14 or later, the inject() function is the simplest way to pull in your ConfigService directly in the module decorator. This function lets you access DI-managed services in contexts where constructor injection isn't available (like module configuration).
- First, import the necessary tools in
appAuth.module.ts:
import { NgModule, inject } from '@angular/core'; import { AuthModule } from 'your-auth-library-path'; // Replace with actual import path import { ConfigService } from './config.service';
- Then, use
inject()to get theConfigServiceinstance and callgetClientID()insideAuthModule.forRoot():
@NgModule({ imports: [ AuthModule.forRoot<any>({ adapter: inject(ConfigService).getClientID() }) ] }) export class AppAuthModule {}
This approach is clean and straightforward—just make sure your ConfigService is properly injectable (either marked with @Injectable({ providedIn: 'root' }) or registered in a module's providers array).
Solution 2: Factory Provider with APP_INITIALIZER (All Angular Versions)
If you're on an older Angular version, or if your getClientID() method is asynchronous (e.g., fetches data from an API), use APP_INITIALIZER to load the client ID before your module initializes.
- Set up a variable to store the client ID and an initialization function in
appAuth.module.ts:
import { NgModule, APP_INITIALIZER } from '@angular/core'; import { AuthModule } from 'your-auth-library-path'; import { ConfigService } from './config.service'; // Variable to hold our client ID let clientID: number; // Initialization function that fetches the client ID from ConfigService function loadClientID(configService: ConfigService) { return () => { // For synchronous calls: clientID = configService.getClientID(); // For asynchronous calls (e.g., HTTP request): // return configService.getClientIDAsync().then(id => clientID = id); }; }
- Register the initializer in your module's
providersarray, then use the storedclientIDinAuthModule.forRoot():
@NgModule({ providers: [ { provide: APP_INITIALIZER, useFactory: loadClientID, deps: [ConfigService], // Tell Angular to inject ConfigService into the factory multi: true // Allows multiple initializers to run } ], imports: [ AuthModule.forRoot<any>({ adapter: clientID }) ] }) export class AppAuthModule {}
This ensures the client ID is loaded before the module finishes initializing, so it's available when AuthModule.forRoot() runs.
Key Notes
- Double-check that
ConfigServiceis properly registered in Angular's DI system. If it's not, the inject calls or factory will fail. - If
getClientID()is asynchronous, stick with Solution 2 and return a Promise from the initialization function—Angular will wait for it to resolve before proceeding.
内容的提问来源于stack exchange,提问作者Mo Halem

