TypeError-o.ngOnDestroy不是函数

时间:2018-08-12 18:47:59

标签: angular angular-cli

在构建我的角度应用程序进行生产时(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)

我正在使用角度和角度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);

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())
  }
}

3 个答案:

答案 0 :(得分:6)

我针对此问题的解决方案是在没有 build-optimizer 的情况下运行构建过程。强制将标志设置为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修复

答案 1 :(得分:0)

在Angluar版本6.1.10更新后出现了相同的问题。

我这样修复:

1)我更新了我的第3部分库(角度日历)
2)删除了node_modules目录(这是重要

答案 2 :(得分:-1)

我遇到了同样的问题,我只想让您知道,这是我为该特定模块导入的问题,这是导致该问题的原因。在这种情况下,是NgBootstrap,与Material一起运行,我取出NgbModule,瞧。我不确定您在做什么,但是请检查您的导入。