I want to check which component/service is calling the method of a certain service, without passing additional parameters. This needs to be checked in the service. Please mention if there's any way that this could be done.
you could use console.trace() inside the service to find out which component is calling that service.
You could do it passing parameters to the called method. But i think this is your without passing additional variables
..
How about event listener? your service could start an Event and you can check this Event in your multiple callers
EDIT:
the callee (who launch the event, maybe your service)
@Component({
selector: 'yourComponent',
templateUrl: './yourComponent.component.html',
styleUrls: ['./yourComponent.component.scss']
})
export class yourClass extends OnInit {
@Output() theEvent = new EventEmitter();
constructor() {}
ngOnInit() {
....
}
eventLauncher() {
this.theEvent.emit();
}
}
<yourHTMLelement (click)="eventLauncher()"></yourHTMLelement>
Who read the caller of event: (in my case, appComponent, so everytime i'll wait for an event, in every page of my application):
export class AppComponent implements OnInit {
@ViewChild('referToElementCaller') theCaller: ElementRef;
.....
funct() {
// the caller is 'this.theCaller'
doSomething();
}
<yourComponent #referToElementCaller (theEvent)="funct()"></yourComponent>
Most straight forward solution is to pass a parameter to the method in service.
Or you can use the pub-sub technique here. You can have some kind of global variable where u can add a entry everytime you call the service method from the component and then read that in the method and clear the value after doing so.
This can not be achieved without passing a parameter because it defies the definition/purpose of a Service.
The relationship between services is minimized to the level that they are only aware of their existence.
The consumer/caller/component/service only knows about the existence of the service whereas, the called service knows nothing about who the callee is?
Read detailed information about Services here - https://en.wikipedia.org/wiki/Service-oriented_architecture
© 2022 - 2024 — McMap. All rights reserved.