Angular 2 : Detect from which component service is called

标签 angular

我有 MyService,它被注入(inject)到 ComponetAComponetB

能否查出MyService.myMethod()是从哪个组件调用的?

@Injectable()
export class MyService {

  public myMethod(): void {
    console.log('called from component: ' + component);
  }
}

最佳答案

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

编辑:以前的建议引用 here不会工作,因为它只在没有严格模式的情况下工作。

关于 Angular 2 : Detect from which component service is called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42949871/

相关文章:

Angular 6 嵌套模板 View

angular - 当 FormArray 为空时 FormArray 应该无效

arrays - Angular2, typescript :How to push array of objects into another array of objects with only a few fields of the object

css - Angular 5 : Position a button half outside mat dialog

javascript - 在 angular2/typescript 中使用 jointjs

angular - 覆盖 Angular 中的浏览器 `back` 按钮功能?

后端的 Angular-CLI 代理不起作用

angular - Angular 2 @Component 语法是 ES6 的一部分吗?

javascript - 如何使用 Angular EventEmitter 将数据从一个组件传递到另一个组件

Angular 4.4.6 的 Angular CLI 版本