Angular config.json 的变量替换 github 操作

variable substitution github actions for Angular config.json

我创建了一个 Angular 应用程序,它使用 RuntimeConfigLoaderService 此服务使用了一个 config.json 文件,我想在部署我的应用程序时更新该文件。

我使用 github 操作创建了部署,操作运行并显示值已更改,但是当我打开应用程序并显示它们时它们是空的

.

看着this,我似乎做对了,我错过了什么:

构建文件

name: Azure Static Web Apps CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened, closed]
    branches:
      - main

jobs:
  build_and_deploy_job:
    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
    runs-on: ubuntu-latest
    name: Build and Deploy Job
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - uses: microsoft/variable-substitution@v1
        with:
          files: './ClientApp/RetroArcadeMachines/src/assets/config.json'
        env:
          read_api_url: 'https://myReadUrl'
          write_api_url: 'https://myWriteUrl'
          write_api_url_auth: 'https://myWriteUrlAuth'
      - name: Build And Deploy
        id: builddeploy
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_ASHY_ISLAND_0772CDD03 }}
          repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
          action: "upload"
          ###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
          # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
          app_location: "./ClientApp/RetroArcadeMachines/" # App source code path
          output_location: "dist/RetroArcadeMachines" # Built app content directory - optional
          ###### End of Repository/Build Configurations ######

  close_pull_request_job:
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    runs-on: ubuntu-latest
    name: Close Pull Request Job
    steps:
      - name: Close Pull Request
        id: closepullrequest
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_ASHY_ISLAND_0772CDD03 }}
          action: "close"

您的 config.json 文件可能被替换得很好,问题是您必须在运行时加载文件。您不能使用 angulars 本机可观察的 http 查询方法。使用承诺。本文详细讲解了如何在运行时读取配置文件。 https://imran3.medium.com/runtime-configuration-for-angular-c9d9082e1de3

我的问题是我 isDevMode() 签入了 AppModule。我将其更改为 environment.production 并且一切正常