我有 MyService,它被注入(inject)到 ComponetA 和 ComponetB。
能否查出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/