我有一个environment.ts文件;

export const environment = { token: '345' };

和一个enviroment.development.ts文件:

export const environment = { token: '123' };

两者都在公共GitHub回购中.我想部署一个应用程序并将令牌值设置为真正的key值,但在Netlify或Vercel上使用环境变量不起作用,因为我为项目设置了它们.我不想在应用程序的任何部分中写入真正的访问令牌.如何部署应用程序并将假 token 价值更改为真实价值?如果我将实际值设为src/environments/environment.development.ts,它就会公开,但我必须这样做,因为它是Netlify或Vercel获取代码的地方.

如果我添加:

  production: true,
  token: '345',

价值观仍然没有改变.

以下是: https://angular.io/guide/build#configure-target-specific-file-replacements

angular.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "flightSearch": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/flight-search",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": ["zone.js"],
            "tsConfig": "tsconfig.app.json",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "src/styles.css",
              "node_modules/primeng/resources/themes/lara-light-blue/theme.css",
              "node_modules/primeng/resources/primeng.min.css"
            ],
            "scripts": [],
            "server": "src/main.server.ts",
            "prerender": true,
            "ssr": {
              "entry": "server.ts"
            }
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.development.ts"
                }
              ]
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "flightSearch:build:production"
            },
            "development": {
              "buildTarget": "flightSearch:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "buildTarget": "flightSearch:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": ["zone.js", "zone.js/testing"],
            "tsConfig": "tsconfig.spec.json",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.css"],
            "scripts": []
          }
        }
      }
    }
  },
  "cli": {
    "analytics": "3921564e-694e-4bc9-aa09-49b8c3772415"
  }
}

推荐答案

在您的项目中将有.gitignore个文件,在这里您可以包含您的environment文件夹,提交甚至不会显示这些文件,它们在您的本地系统中是安全的,并且您可以使用它们从本地部署.

另一种方法是使用Deploy keys(github),您可以安全地存储密钥,然后在部署代码时使用带有node.js的脚本创建文件.

How to keep your secrets from your source code in an Angular project ?!

Angular相关问答推荐

iOS Mobile Safari - HTML5视频覆盖一切

无法执行CanDeactivateFn Karma Test Angular 16

如何在Angular 17中使用Angular—calendum?

导致无限请求的Angular HttpInterceptor和HttpClientModule

在Angular 应用中混合使用较少和较粗俗的文件

在Angular 为17的独立零部件中使用@NGX-平移

截获火灾前调用公共接口

如何在独立应用程序中全局禁用基于媒体查询的Angular 动画?

具有两个订阅方法的 Angular 16 takeUntilDestroyed 运算符

使用 RXJS 进行空闲/不活动跟踪

Summernote 文本区域的输入字段文本验证失败

如何以Angular 显示动态视图模型

为什么 HttpParams 在 Angular 4.3 中的多行中不起作用

CORS 策略和 .NET Core 3.1 的问题

错误:formControlName 必须与父 formGroup 指令一起使用.您需要添加一个 formGroup 指令

material Angular手风琴header/title高度

为什么我们需要`ngDoCheck`

AOT - Angular 6 - 指令 SomeComponent,预期 0 个参数,但go 有 1 个参数

MatToolbar 与 Angular 9 一起使用时抛出错误

如何在 Angular 中使用带有 SASS 的 Bootstrap 4