角度2:检测从哪个组件服务调用 [英] Angular 2: Detect from which component service is called

查看:53
本文介绍了角度2:检测从哪个组件服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 MyService ,它已注入到 ComponetA ComponetB 中.

I have MyService which is injected into ComponetA and ComponetB.

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

Is it possible to find out from which component is the MyService.myMethod() called?

@Injectable()
export class MyService {

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

推荐答案

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

Only solution would be to use a parameter which would be passed to the called function.

以前的建议是在此处 a>不起作用,因为它只能在没有严格模式的情况下起作用.

Previous suggestion to refer here would not work because it only works without strict mode.

这篇关于角度2:检测从哪个组件服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆