SSR在Angular中采用了错误的环境

Vyt*_*nas 5 environment-variables server-side-rendering ssr angular angular6

我有适用于SSR的Angular 6应用程序.我注意到它在server.js中的SSR上需要一次错误的environmanet变量文件(environment.ts)(没有SSR时没有发生)

这是编译server.js的和平, var environment_1 = __webpack_require__(/*! ../../../../environments/environment */ "./src/environments/environment.ts");这很自然,因为编译浏览器时angular.json交换文件

 "fileReplacements": [
                            {
                                "replace": "src/environments/environment.ts",
                                "with": "src/environments/environment.prod.ts"
                            }
                        ]
Run Code Online (Sandbox Code Playgroud)

然而,一旦webpack编译服务器,它只需要enironment.ts这是dev配置

/***/ "./src/environments/environment.ts":
/*!*****************************************!*\
  !*** ./src/environments/environment.ts ***!
  \*****************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.
Object.defineProperty(exports, "__esModule", { value: true });
exports.environment = {
    production: false,
    apiUrl: 'dev url',
    googleMapsApiKey: 'dev key'
};


/***/ }),
Run Code Online (Sandbox Code Playgroud)

你也可以看到角度过时的建议使用,ng build --env=prod但我ng build --configuration=prod也尝试过ng build --prod.

任何想法如何解决这个问题?

Вик*_*кий 7

我已经解决了这个问题.您需要检查angular.json文件.在服务器部分,您需要"server": { "builder": "@angular-devkit/build-angular:server", "options": { "outputPath": "./dist/server", "main": "./src/client/main.server.ts", "tsConfig": "./src/client/tsconfig.server.json" }, "configurations": { "production": { "fileReplacements": [ { "replace": "src/client/environments/environment.ts", "with": "src/client/environments/environment.prod.ts" } ] }, "cloud": { "fileReplacements": [ { "replace": "src/client/environments/environment.ts", "with": "src/client/environments/environment.cloud.ts" } ] } } }


Vyt*_*nas 2

我已经解决了。我没有将带有文件替换的配置添加到 angular.json 文件中的服务器部分。