Angular 2:检测从哪个组件服务被调用

Tom*_*rik 5 angular

我有MyService被注入ComponetAComponetB

是否可以找出MyService.myMethod()是从哪个组件调用的?

@Injectable()
export class MyService {

  public myMethod(): void {
    console.log('called from component: ' + component);
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 3

唯一的解决方案是使用将传递给被调用函数的参数。

编辑:之前在此处引用的建议不起作用,因为它只能在没有严格模式的情况下起作用。