gpt4 book ai didi

angular - 在生产模式下将 Angular 部署到 Heroku

转载 作者:太空狗 更新时间:2023-10-29 18:09:26 27 4
gpt4 key购买 nike

很抱歉,如果这个问题已经在某个地方得到了回答。

我能够将 Angular 应用程序部署到 heroku,但尽管采取了本文中的步骤,它仍无法在生产模式下运行。

https://medium.com/@hellotunmbi/how-to-deploy-angular-application-to-heroku-1d56e09c5147

我们在环境文件夹中有两个文件:

  • 环境.prod.ts
  • 环境.ts

从本质上讲,Heroku 似乎使用了名为 environment.ts 的第二个环境文件,而不是使用 environment.prod.ts

在environment.ts中,有一个env变量:

// This file can be replaced during build by using the `fileReplacements` array.
// `ng build --prod` replaces `environment.ts` with `environment.prod.ts`.
// The list of file replacements can be found in `angular.json`.

export const environment = {

production: false,

};

fileReplacements 也已正确设置。

environment.prod.ts 文件也包含变量 production 并且值为 true

我们知道这不是使用以下简单代码的生产模式:

export class SettingsComponent implements OnInit {

environmentIsProduction: boolean = false;

constructor() {

}

ngOnInit() {
this.environmentIsProduction = environment.production;
console.warn('Environment is PRODUCTION: ' + environment.production);
}

}

是否有人可以就我们可能忘记的步骤提出建议?

这是整个 package.json 文件

{
"name": "angular-frontend",
"version": "0.0.0",
"engines": {
"node": "8.11.2",
"npm": "6.2.0"
},
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"postinstall": "ng build --aot --prod"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.1.0",
"@angular/cdk": "^7.2.1",
"@angular/cli": "~6.2.3",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/compiler-cli": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"@ng-bootstrap/ng-bootstrap": "^3.2.2",
"@swimlane/ngx-datatable": "^14.0.0",
"angular-notifier": "^4.1.1",
"angular-svg-icon": "^7.0.1",
"angularx-social-login": "^1.2.6",
"bootstrap": "^4.1.3",
"core-js": "^2.5.4",
"express": "^4.16.4",
"flag-icon-css": "^3.3.0",
"jquery": "^1.9.1",
"jw-bootstrap-switch-ng2": "^2.0.2",
"ng-connection-service": "^1.0.4",
"ng-pick-datetime": "^6.0.16",
"ng2-currency-mask": "^5.3.1",
"ng2-nouislider": "^1.7.12",
"ngx-bootstrap": "^3.0.1",
"ngx-flag-icon-css": "^1.0.1",
"ngx-webstorage-service": "^3.1.1",
"nouislider": "^11.0.0",
"path": "^0.12.7",
"popper.js": "^1.14.4",
"rellax": "^1.7.0",
"rxjs": "~6.2.0",
"typescript": "~2.9.2",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.8.0",
"@angular/cli": "~6.2.3",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.3.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~2.9.2"
}
}

最佳答案

我希望这对某人有帮助......

终于找到解决方案:https://devcenter.heroku.com/articles/nodejs-support#heroku-specific-build-steps

在 package.json 中,有必要更改以下行:

"postinstall": "ng build --aot --prod"

"heroku-postbuild": "ng build --configuration=production"

虽然原始命令确实经历了生产构建和文件名混淆等所有步骤,修剪......然后它随后运行正常的 ng 构建。

在部署到 Heroku 期间,我在日志中注意到了这种行为。

一旦使用 heroku-postbuild,构建只发生一次......用于生产。

关于angular - 在生产模式下将 Angular 部署到 Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55182629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com