I have an angular service that is just supposed to monitor for route changed events. When I try to add this service at the AppModule level I get a compilation error that says something like "Service is declared but its value is never read'. I understand what the error means and get why its popping up, the issue is that I don't need to call any of the service functions from outside the service. Here is a snippet similar to what I have going on for my Service
@Injectable()
export class MyService {
constructor(
private router: Router,
private route: ActivatedRoute
) {
this.router.events
.filter(e => e instanceof NavigationEnd)
.forEach(e => this.onRouteChanged(this.route.root.firstChild.snapshot.data.analytics.pageName));
}
onRouteChanged(pageName: string) {
// do some stuff with the page name
}
}
This is what I have in my app.module.ts. If I don't include the ngOnInit function that just does a console log with my service then it will fail to compile.
export class AppModule {
constructor(
private myService: MyService // if this line is not here then my service doesn't load at all and the route changed event is not tracked
) { }
ngOnInit() {
console.log(this.myService);
}
}
If I do the console.log I can see the route changed event firing so I know the service gets loaded. Is it possible to do include the MyService reference without requiring the console.log?