【问题标题】:Angular 4: Error: Uncaught (in promise): TypeError: X is not a constructorAngular 4:错误:未捕获(承诺):TypeError:X不是构造函数
【发布时间】:2018-02-01 04:05:05
【问题描述】:

我正在开发一个 Angular 4 应用程序。在我运行该应用程序时,它会引发此错误 -

ERROR Error: Uncaught (in promise): TypeError: index_1.EmployeeBase is not a constructor
TypeError: index_1.EmployeeBase is not a constructor
    at new EmployeeDetailComponent (employee-detail.component.ts:112)
    at createClass (core.umd.min.js:97)
    at createDirectiveInstance (core.umd.min.js:97)
    at createViewNodes (core.umd.min.js:132)
    at createRootView (core.umd.min.js:132)
    at Object.createProdRootView [as createRootView] (core.umd.min.js:132)
    at ComponentFactory_.create (core.umd.min.js:209)
    at ComponentFactoryBoundToModule.create (core.umd.min.js:188)
    at ViewContainerRef_.createComponent (core.umd.min.js:210)
    at RouterOutlet.activateWith (router.umd.min.js:57)
    at new EmployeeDetailComponent (employee-detail.component.ts:112)
    at createClass (core.umd.min.js:97)
    at createDirectiveInstance (core.umd.min.js:97)
    at createViewNodes (core.umd.min.js:132)
    at createRootView (core.umd.min.js:132)
    at Object.createProdRootView [as createRootView] (core.umd.min.js:132)
    at ComponentFactory_.create (core.umd.min.js:209)
    at ComponentFactoryBoundToModule.create (core.umd.min.js:188)
    at ViewContainerRef_.createComponent (core.umd.min.js:210)
    at RouterOutlet.activateWith (router.umd.min.js:57)
    at u (zone.min.js:1)
    at u (zone.min.js:1)
    at zone.min.js:1
    at e.invokeTask (zone.min.js:1)
    at Object.onInvokeTask (core.umd.min.js:41)
    at e.invokeTask (zone.min.js:1)
    at r.runTask (zone.min.js:1)
    at o (zone.min.js:1)
    at t.invokeTask [as invoke] (zone.min.js:1)
    at d (zone.min.js:1)

我搜索了一个解决方案,然后才知道,这个问题与 polyfills 有关。有人建议将“es6-shim”升级到“0.35.0”版本。但是我已经在使用该版本,如下所示,但问题仍未解决。

"dependencies": {
    "@angular/animations": "^4.4.3",
    "@angular/common": "^4.4.3",
    "@angular/compiler": "^4.4.3",
    "@angular/core": "^4.4.3",
    "@angular/forms": "^4.4.3",
    "@angular/http": "^4.4.3",
    "@angular/platform-browser": "^4.4.3",
    "@angular/platform-browser-dynamic": "^4.4.3",
    "@angular/router": "^4.4.3",
    "@angular/upgrade": "^4.4.3",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.5",
    "@ngui/auto-complete": "^0.14.4",
    "bootstrap": "^4.0.0-beta",
    "core-js": "^2.5.1",
    "jquery": "^3.2.1",
    "ng2-bootstrap-modal": "^1.0.1",
    "ng2-translate": "^5.0.0",
    "ng2-validation": "^4.2.0",
    "ngx-contextmenu": "^1.3.3",
    "ngx-infinite-scroll": "^0.5.2",
    "normalize.css": "^7.0.0",
    "popper.js": "^1.12.5",
    "rxjs": "5.1.0",
    "systemjs": "0.19.40",
    "traceur": "^0.0.96",
    "underscore": "^1.8.3",
    "zone.js": "^0.8.17"
  },
  "devDependencies": {
    "@angular/language-service": "^4.4.3",
    "@types/jasmine": "2.5.45",
    "@types/node": "^6.0.88",
    "browser-sync": "^2.18.13",
    "codelyzer": "~3.0.1",
    **"es6-shim": "^0.35.3",**
    "gulp": "^3.9.1",
    "gulp-header": "^1.8.9",
    "gulp-less": "^3.3.2",
    "gulp-minify-css": "^1.2.4",
    "gulp-rename": "^1.2.2",
    "gulp-typescript": "^3.2.2",
    "gulp-uglify": "^3.0.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage": "^1.1.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-htmlfile-reporter": "^0.3.5",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-typescript": "^3.0.8",
    "karma-typescript-es6-transform": "^1.0.2",
    "protractor": "~5.1.2",
    "reflect-metadata": "^0.1.10",
    "require-dir": "^0.3.2",
    "run-sequence": "^2.2.0",
    "systemjs-builder": "^0.16.11",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }

打字稿代码下方是引发错误的地方。此行位于员工详细信息组件内 -

 employee: IEmployeeBase = new EmployeeBase();

编译出来的 JS 代码是这样的 -

this.employee = new index_1.EmployeeBase();

有人遇到过类似的问题吗?任何帮助或指示都会非常有帮助。 谢谢。

这是我的类定义 -

export class EmployeeCore {
    Name: string;
    Id: string;
    FullName: string;
    FirstName: string;
    LastNamePrefix: string;

    constructor(obj?: EmployeeCore) {
        this.Name = obj && obj.Name || '';
        this.Id = obj && obj.Id || '';
        this.FullName = obj && obj.FullName || '';
        this.FirstName = obj && obj.FirstName || '';
        this.LastNamePrefix = obj && obj.LastNamePrefix || '';
    }
}

export class EmployeeBase extends EmployeeCore  {
    BirthDate: Date;
    BusinessUnit: IBusinessUnitReference;
    CompanyHistory: Array<IEmployeeCompanyHistoryBase>;
    
    constructor(obj?: EmployeeBase) {
        super(obj);
        this.BirthDate = obj && obj.BirthDate || null;
        this.BusinessUnit = obj && obj.BusinessUnit || new BusinessUnitReference();
        this.CompanyHistory = obj && obj.CompanyHistory || [];
        
    }
}

【问题讨论】:

  • 你能说明你是如何声明EmployeeBase的吗?
  • 出现此错误是因为您试图将变量或对象用作构造函数。请按照@MondKin 的建议出示您的EmployeeBase

标签: javascript angular ecmascript-6 polyfills es6-shim


【解决方案1】:

最后 - 罪魁祸首是 SystemJS 与 Barrels 不兼容。我发现 System JS 不适用于 Angular 应用程序中的 Barrels(使用 index.js)。

无法解析循环依赖,使用Barrels时无法正确加载模块。

我可以通过不使用 Barrels 导入对象来解决它,而是使用 JS 文件的完整路径。

【讨论】:

    【解决方案2】:

    可能您试图将对象或变量用作构造函数,但对象和变量不是构造函数。 尝试导入
    import 'core-js'; import 'zone.js/dist/zone-microtask';

    参考这个可能对你有帮助 https://airbrake.io/blog/javascript-error-handling/x-not-constructor-typeerror

    【讨论】:

    • @YogeshPatel in component.ts
    【解决方案3】:

    试试这个..

    ng build --prod --optimization=false

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2020-11-20
      • 1970-01-01
      • 2022-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      相关资源
      最近更新 更多