I need to use logger service in my Angular2 app, but it's rather uncomfortable to write (I'm using ES6 with Angular2):
import { LoggerService } from '../services/logger-service';
...
constructor(@Inject(LoggerService) logger) {
this.logger = logger;
}
in every component or service where I want to use it. Is there a way to inject LoggerService
globally and use anywhere in the app? I haven't found a way to do it.
This is how my logger looks:
import {Injectable} from '~/node_modules/@angular/core';
@Injectable()
export class LoggerService {
constructor() {
if (appConfig.dev) {
this.log = console.log.bind(console);
this.error = console.error.bind(console);
this.info = console.info.bind(console);
} else {
this.log = this.error = this.info = () => null;
}
}
}
And example of component where I want to use it in:
import { Component, Inject } from '~/node_modules/@angular/core';
import { LoggerService } from '../services/logger-service';
@Component({
'selector': 'activities',
'template': template,
'styles': [style]
})
export class ActivitiesComponent {
constructor(@Inject(LoggerService) logger){
this.logger = logger;
}
someAction() {
this.logger.log('hello');
}
}