/**
 * Auto log
 * Auto unsubscrible
 */
export function AutoComponent(obs$ = []) {
  return function (constructor: Function) {
    console.log(constructor, "log");
    const original_Init = constructor.prototype.ngOnInit;
    constructor.prototype.ngOnInit = function () {
      console.log("Enter page " + this.name, "log");
      if (original_Init) {
        original_Init.apply(this);
      }
    };
    const origial_Destroy = constructor.prototype.ngOnDestroy;
    constructor.prototype.ngOnDestroy = function () {
      for (const prop in this) {
        const property = this[prop];
        if (
          property &&
          typeof property.unsubscribe === "function" &&
          !obs$.includes(property)
        ) {
          obs$.push(property);
        }
      }
      for (const ob$ of obs$) {
        ob$.unsubscribe();
      }
      console.log(origial_Destroy, "log");
      if (origial_Destroy) {
        origial_Destroy.apply(this);
      }
      console.log("Exit page " + this.name, "log");
    };
  };
}
posted on 2020-05-03 14:19  码农-编程小子  阅读(273)  评论(0编辑  收藏  举报