从Highcharts.wrap中删除扩展的Wrap方法

egmofgnx  于 2022-11-11  发布在  Highcharts
关注(0)|答案(1)|浏览(184)

如何手动删除/撤消Highcharts类原型上的 Package ?
我目前在Angular中的一个类中有这个,看起来即使在组件被销毁后,wrap也会将我的方法作为闭包保留。

import * as Highcharts from 'highcharts';
export class ExampleComponent implements OnDestroy
     constructor() {
         function logRefresh(H) {
             H.wrap(H.Tooltip.prototype, 'refresh', function(proceed, point) {
                 proceed.apply(this, Array.prototype.slice.call(arguments, 1));
                 console.log(proceed, point);
             });
         }
         logRefresh(Highcharts);
     }

    ngOnDestroy(): void {
        ...
    }
}
zrfyljdw

zrfyljdw1#

wrap方法覆盖了Highcharts原型中的一个原始函数,所以这是一个永久性的改变。你可以在这里检查这个方法是如何工作的。
作为一种解决方案,您可以将原始函数存储在某个位置,例如:

H.wrap(H.Tooltip.prototype, 'refresh', function(proceed) {
  console.log('refresh');

  if (!H.Tooltip.prototype.refreshOriginal) {
    H.Tooltip.prototype.refreshOriginal = proceed;
  }

  proceed.apply(this, Array.prototype.slice.call(arguments, 1));
});

并在需要时恢复它。

const tooltipProto = H.Tooltip.prototype;
// restore original refresh function
tooltipProto.refresh = tooltipProto.refreshOriginal;
delete tooltipProto.refreshOriginal;

现场演示:http://jsfiddle.net/BlackLabel/nfqcwhk6/
文件:https://www.highcharts.com/docs/extending-highcharts/extending-highcharts

相关问题