如何正确地将 Angular index.html 文件替换/重命名为 index.php?

Pri*_*ceG 3 php angular

在我的 Angular 项目中,我需要在 index.html 文件中使用 PHP。我已经将其重命名为index.php 并更新了 angular.json 以在索引属性中使用index.php。

我的问题是,运行ng build命令后,dist 目录中生成的index.php 文件中的PHP 代码被注释。

<!--?php
$websiteCode = getenv('SITE_CODE');
?--><!DOCTYPE html><html lang="en"><head>
  <meta charset="utf-8">
  <title>ClientPortal</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.31d6cfe0d16ae931b73c.css"></head>
<body>
  <!--?=$websiteCode?-->
  <app-root></app-root>
<script src="runtime.f3b55109ab97bda870bd.js" defer></script><script src="polyfills.a58f45ef9ccddb12523d.js" defer></script><script src="main.6629fcd1d3960ef0deb9.js" defer></script>

</body></html>
Run Code Online (Sandbox Code Playgroud)

是否有我需要更新或指定的配置,以便 PHP 代码不会被注释?我正在使用 Angular 12。这在我现有的使用 Angular 7 的 Angular 项目中运行良好。

asi*_*iby 5

这种行为似乎是一个错误。这是由 CSS 优化器引起的。禁用该inlineCritical解决方案可以解决问题(请参阅https://angular.io/guide/workspace-config#styles-optimization-options)。

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "fubar": {
      "projectType": "library",
      ...
    },
    "acme": {
      "projectType": "application",
      "schematics": { ... },
      ...
      "architect": {
        "build": {
          ...
          "configurations": {
            "production": {
              ...
              "optimization": {  <-- ADDING THIS HAS SOLVED THE ISSUE FOR ME.
                "styles": {
                  "inlineCritical": false
                }
              }
            },
            "development": {
              ...
            }
          },
          "defaultConfiguration": "..."
        },
        "serve": {
          ...
        },
        "extract-i18n": {
          ...
        },
        "test": {
          ...
        }
      }
    }
  },
  "defaultProject": "acme"
}
Run Code Online (Sandbox Code Playgroud)

重要的是要注意构建时间的差异,这表明禁用的优化不是一项简单的任务。

在此输入图像描述