首页 > 解决方案 > 将环境变量传递给 angular.json 文件。(角7)

问题描述

我正在尝试在 angular.json 文件中使用环境变量。我怀疑这是不可能的,但我想看看是否有人知道。我用它来指向服务器生成的信号器文件(问题是在开发与暂存时它是不同的baseUrl)。所以... angular.json

"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ab-inbev-web2": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.json",
            "assets": [
              "src/assets",
              "src/favicon.ico"
            ],

            "scripts": [
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              "http://site-dev.com/signalr/hubs"
and when on staging
              "http://site-stage.com/signalr/hubs"
            ]

环境.ts

export const environment = {
  baseUrlWithSlash: 'http://site-prod.com/',
  logoffWarningTimeoutSeconds: 600,
  production: true,
  searchDelimiters: /[,;\n]+/
};

environment-local.ts
  export const environment = {
      baseUrlWithSlash: 'http://site-dev.com/',
      logoffWarningTimeoutSeconds: 600,
      production: true,
      searchDelimiters: /[,;\n]+/
    };

等等...

我试图避免对其进行硬编码并尝试在本地运行时让它工作。我想也许有一种方法可以嗅探 angular.json 文件中的位置并执行 if 语句。

标签: angularenvironment-variablesbuild-process

解决方案


推荐阅读