typescript - Angular 4 NgModule - Menyuntikkan layanan dengan nama kelas target ke layanan lain
Saya ingin tahu apakah ada cara untuk mengakses nama kelas target selama dependency injection Angular 4. Saya mencoba melakukan itu karena saya ingin sistem logging saya mengetahui sumber logging dan mencetaknya tanpa harus menyebutkannya setiap kali.
Berikut beberapa konteks untuk masalah saya:
Ini adalah cuplikan dari pencatat saya
@Injectable()
export class Logger {
constructor(private source: string, (...)) { }
}
Ini adalah cuplikan dari layanan saya
@Injectable()
export class SomeService {
constructor(private logger: Logger, (...)) {
// I know i could initialize the logger here and set the name of the service, but I would like to do it by injection, if possible
}
}
Apa yang ingin saya lakukan
export function factoryFunc() {
return (injectionContext : InjectionContext): string => {
/* InjectionContext doesn't exist, its just theorical and to explain
what I'm trying to achieve, which is to access an injection context in
the factory func and the target class name
*/
var className = injectionContext.getInjectionTargetClassName();
return new Logger(className);
};
};
@NgModule({
(...),
providers: [
SomeService,
{ provide: Logger , useFactory: factoryFunc() },
]
})