在来自外部 class 的 Zone 回调上绑定方法

Bind methods on Zone callbacks from an external class

上下文

我正在开始一个学生项目,它是 angular2 的度量工具。

我实际上正在研究概念证明,证明我可以在运行时仅使用静态寄存器方法 myLibClass.registerComponents([Component1, Component2,...]) 获得组件的指标,该方法将具有这样的实现(伪-代码):

registerComponents(components:any[]):void{
    components.forEach(component => {
        //Register metric methods on zone foreTask and afterTask
    });
}

问题

如何获取 angular2 的全局区域?我想将我的方法绑定到 ng2 区域,以便能够检查每次调用的执行时间,NgZone 不提供执行此类操作的工具。

也欢迎从外部 class 度量组件的另一个想法

更新:

我做了一些研究,发现了如何使用 ̀ Zone.current` 获取全局 angular2 区域。

问题依然存在,因为它的第二部分(最难的部分)还没有实现:

I want to bind my methods on ng2 zone in order to be able to check execution time for each call and NgZone does not provide the tools to do such a thing.

执行此操作的唯一方法是分叉 angular2 区域并用我的分叉区域替换 angular 使用的实际区域(这是我看到的唯一方法) .

这可能吗?分叉区域不是问题,但我无法在 angular2.

上连接它

因为我需要的信息比 null 多(正如您在 the source code of ngZone 上看到的那样,每个触发的事件都是用 null 触发的,这不允许我获取信息我需要最后一个微任务,比如呼叫名称或呼叫者),我需要一个自定义区域,它将被 angular2 使用,但我无法扩展 NgZone,因为它没有使用 DI 进行实例化因此 angular2 使用的区域仍然是 NgZone。

如果您想要 运行 具有覆盖 ngZone 的应用程序,您可以执行以下技巧。

const platform = platformBrowserDynamic();
const zone = new MyZone();
platform['_bootstrapModuleWithZone'](AppModule, [], zone);

它在 ng1 升级适配器中使用

可惜是私有方法

另见

  • Changing shared data between root modules