TypeError - o.ngOnDestroy 不是函数

2024-04-08

在构建用于生产的角度应用程序时(ng服务没有问题),我在加载子模块之一时收到以下错误:

ERROR Error: Uncaught (in promise): TypeError: o.ngOnDestroy is not a function
TypeError: o.ngOnDestroy is not a function
    at Fi (core.js.pre-build-optimizer.js:9573)
    at Mi (core.js.pre-build-optimizer.js:9541)
    at Ri (core.js.pre-build-optimizer.js:9531)
    at fo (core.js.pre-build-optimizer.js:10593)
    at go (core.js.pre-build-optimizer.js:10719)
    at vo (core.js.pre-build-optimizer.js:10662)
    at fo (core.js.pre-build-optimizer.js:10591)
    at go (core.js.pre-build-optimizer.js:10719)
    at mo (core.js.pre-build-optimizer.js:10641)
    at fo (core.js.pre-build-optimizer.js:10592)
    at Fi (core.js.pre-build-optimizer.js:9573)
    at Mi (core.js.pre-build-optimizer.js:9541)
    at Ri (core.js.pre-build-optimizer.js:9531)
    at fo (core.js.pre-build-optimizer.js:10593)
    at go (core.js.pre-build-optimizer.js:10719)
    at vo (core.js.pre-build-optimizer.js:10662)
    at fo (core.js.pre-build-optimizer.js:10591)
    at go (core.js.pre-build-optimizer.js:10719)
    at mo (core.js.pre-build-optimizer.js:10641)
    at fo (core.js.pre-build-optimizer.js:10592)
    at P (zone.js:814)
    at P (zone.js:771)
    at zone.js:873
    at e.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js.pre-build-optimizer.js:3815)
    at e.invokeTask (zone.js:420)
    at t.runTask (zone.js:188)
    at d (zone.js:595)
    at t.invokeTask [as invoke] (zone.js:500)
    at k (zone.js:1540)

我正在使用 Angular 和 Angular cli 6.1.2。有什么想法可能会导致这种情况吗?

下面是在生成的 main.js 文件中找到 ngOnDestroy 的两个代码片段:

var pi = function () {
  function t(t, e, n, r) {
    this._moduleType = t, this._parent = e, this._bootstrapComponents = n, this._def = r, this._destroyListeners = [], this._destroyed = !1, this.injector = this, function (t) {
      for (var e = t._def, n = t._providers = new Array(e.providers.length), r = 0; r < e.providers.length; r++) {
        var i = e.providers[r];
        4096 & i.flags || void 0 === n[r] && (n[r] = qr(t, i))
      }
    }(this)
  }

  return t.prototype.get = function (t, e, n) {
    void 0 === e && (e = D.THROW_IF_NOT_FOUND), void 0 === n && (n = 0);
    var r = 0;
    return 4 & n ? r |= 1 : 2 & n && (r |= 4), Hr(this, {token: t, tokenKey: $n(t), flags: r}, e)
  }, Object.defineProperty(t.prototype, "instance", {
    get: function () {
      return this.get(this._moduleType)
    }, enumerable: !0, configurable: !0
  }), Object.defineProperty(t.prototype, "componentFactoryResolver", {
    get: function () {
      return this.get(Bt)
    }, enumerable: !0, configurable: !0
  }), t.prototype.destroy = function () {
    if (this._destroyed)throw new Error("The ng module " + k(this.instance.constructor) + " has already been destroyed.");
    this._destroyed = !0, function (t, e) {
      for (var n = t._def, r = new Set, i = 0; i < n.providers.length; i++)if (131072 & n.providers[i].flags) {
        var o = t._providers[i];
        if (o && o !== Fr) {
          var a = o.ngOnDestroy;
          "function" != typeof a || r.has(o) || (a.apply(o), r.add(o))
        }
      }
    }(this), this._destroyListeners.forEach(function (t) {
      return t()
    })
  }, t.prototype.onDestroy = function (t) {
    this._destroyListeners.push(t)
  }, t
}(), fi = $n(function () {
}), di = $n(ke), yi = $n(Oe), mi = $n(Ae), vi = $n(je), gi = $n(Pe), _i = $n(D), bi = $n(P);

and

function Fi(t, e, n, r) {
  var i = Hn(t, e);
  if (i) {
    var o = i.instance;
    o && (Gn.setCurrentNode(t, e), 1048576 & n && Bn(t, 512, r) && o.ngAfterContentInit(), 2097152 & n && o.ngAfterContentChecked(), 4194304 & n && Bn(t, 768, r) && o.ngAfterViewInit(), 8388608 & n && o.ngAfterViewChecked(), 131072 & n && o.ngOnDestroy())
  }
}

我对这个问题的解决方案是在没有构建优化器。要么强制标志为 false:

ng build --prod --build-optimizer=false

或者在 angular.json 的配置部分将其设置为 false:

"configurations": {
  "production": {
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": false,
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ],
    "serviceWorker": true
  }

Angular 7 解决了问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TypeError - o.ngOnDestroy 不是函数 的相关文章

随机推荐