Angular在执行“build --prod”后出现“Uncaught TypeError: e不是构造函数”(在ng serve上工作)

12

我在将我的Angular应用程序上传到生产环境时遇到了问题。该应用程序在开发环境中运行良好,并且在以前的生产环境中也可以正常工作。

构建我的应用程序时,我正在使用:

ng build --prod --base-href="/site/" 

应用程序已构建,但存在以下警告:

WARNING in ./src/app/eco-header/eco-header.component.ngfactory.js 138:299-319 "export 'MAT_ICON_LOCATION' (imported as 'i9') was not found in '@angular/material/icon'

WARNING in ./node_modules/@angular/material/dialog/typings/index.ngfactory.js 20:1247-1265 "export 'ScrollingModule' (imported as 'i7') was not found in '@angular/cdk/scrolling'

WARNING in ./node_modules/@angular/material/dialog/typings/index.ngfactory.js 20:1267-1285 "export 'ScrollingModule' (imported as 'i7') was not found in '@angular/cdk/scrolling'

WARNING in ./node_modules/@angular/material/icon/typings/index.ngfactory.js 20:327-347 "export 'MAT_ICON_LOCATION' (imported as 'i1') was not found in '@angular/material/icon'

当我尝试加载页面时,出现以下错误:
Uncaught TypeError: e is not a constructor
    at main.1ebe20842d3713f62535.js:1
    at Wl (main.1ebe20842d3713f62535.js:1)
    at main.1ebe20842d3713f62535.js:1
    at new t (main.1ebe20842d3713f62535.js:1)
    at Object.dr [as createNgModuleRef] (main.1ebe20842d3713f62535.js:1)
    at e.create (main.1ebe20842d3713f62535.js:1)
    at main.1ebe20842d3713f62535.js:1
    at e.invoke (polyfills.c72d3210425a88b28b6d.js:1)
    at Object.onInvoke (main.1ebe20842d3713f62535.js:1)
    at e.invoke (polyfills.c72d3210425a88b28b6d.js:1)

我的tsconfig文件:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "allowJs": true,
        "sourceMap": true
    }
}

我的package.json文件:

{   "name": "idea-ecomanager-frontend",   "version": "0.0.0",   "scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"   },   "private": true,   "dependencies": {
"@angular/animations": "^7.0.1",
"@angular/cdk": "^7.0.2",
"@angular/common": "^7.0.1",
"@angular/compiler": "^7.0.1",
"@angular/core": "^7.0.1",
"@angular/forms": "^7.0.1",
"@angular/http": "^7.0.1",
"@angular/material": "^7.0.2",
"@angular/platform-browser": "^7.0.1",
"@angular/platform-browser-dynamic": "^7.0.1",
"@angular/router": "^7.0.1",
"angularcli": "^6.0.1",
"bootstrap": "^4.1.3",
"core-js": "^2.5.4",
"ngx-loading": "^3.0.1",
"ngx-toastr": "^9.1.1",
"rxjs": "^6.3.3",
"zone.js": "^0.8.26"   },   "devDependencies": {
"@angular-devkit/build-angular": "~0.6.0",
"@angular/cli": "^6.0.8",
"@angular/compiler-cli": "^7.0.1",
"@angular/language-service": "^7.0.1",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1",
"typescript": "~3.1.3"   } }

有什么想法为什么会发生这种情况吗?
2个回答

16

经过长时间的尝试,我发现 Angular CLI 存在一个未解决的 bug。

https://github.com/angular/angular-cli/issues/9929

ng build --prod --base-href="/site/" --aot=false --build-optimizer=false

目前使用 --aot=false --build-optimizer=false 可以临时解决这个问题。

我将继续更新此帖子,直到 Angular 团队提供官方信息和解决方案。


这个修复了我在从Angular 6升级到Angular 7后卡了几天的错误。 - Dave Shinkle
1
但是这样做,您将失去所有构建优化和树摇动的好处。该问题与 angular-cli 版本有关。尝试全局降级或升级它。 - Akash

1
我们也遇到了同样的问题。一旦删除所有节点模块并逐个安装节点模块,然后在安装每个节点模块后输入ng build --prod,这可能有所帮助。在一个项目中,我们在ngx password toggle节点模块中遇到了该错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接