After adding provider also getting the NullInjectorError in Angular 14
Code shown below
app.module.ts
import { LogService } from './services/log.service';
import { LogType, Config } from './interface/config';
import { loggerServiceFactory } from './logfactory';
export const config = {
apiUrl: 'http://localhost:3000',
logType: LogType.Default
}
providers: [
{provide: LogService, useFactory: loggerServiceFactory, deps:[config]}
],
loggerServiceFactory
import { AppConfig, LogType } from "./interface/config";
import { ClientService } from "./services/client.service";
import { LogService } from "./services/log.service";
export const loggerServiceFactory = (config: AppConfig) => {
if(config.logType === LogType.Client) {
return new ClientService();
}
return new LogService();
}
LogService
import { Injectable } from '@angular/core';
@Injectable()
export class LogService {
protected id = 0;
constructor() {
this.id = Math.random() * 100;
console.log(`LoggerService ${this.id} created`)
}
log(message: string) {
console.log(`LoggerService ${this.id} log: ${message}`);
}
}
app.component.ts
constructor(private log: LogService){
this.log.log('App component constructor');
}