简体   繁体   English

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

[英]How can I properly replace/rename Angular index.html file to index.php?

In my Angular project, I need to use PHP in index.html file.在我的 Angular 项目中,我需要在 index.html 文件中使用 PHP。 I already renamed it to index.php and updated angular.json to use index.php in index property.我已经将它重命名为 index.php 并更新了 angular.json 以在 index 属性中使用 index.php。

My problem is, after running ng build command, the PHP codes in the generated index.php file in dist directory are commented.我的问题是,运行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>

Is there a configuration I need to update or specify so that PHP codes will not be commented?是否有我需要更新或指定的配置以便 PHP 代码不会被注释? I am using Angular 12. This is working fine in my existing Angular projects using Angular 7.我正在使用 Angular 12。这在我使用 Angular 7 的现有 Angular 项目中运行良好。

This behaviour seems to be a bug.这种行为似乎是一个错误。 It's caused by a CSS optimizer.它是由 CSS 优化器引起的。 Disabling the inlineCritical fixes the issue (see https://angular.io/guide/workspace-config#styles-optimization-options ).禁用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"
}

It's important to note the difference in build time which indicate that the optimization that got disabled was not a trivial task.重要的是要注意构建时间的差异,这表明被禁用的优化不是一项简单的任务。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在cordova中使用index.php而不是index.html? - how can i use index.php instead of index.html in cordova? 如果我将 index.html 重命名为 index.php,CSS 样式就会消失 - If I rename an index.html to index.php, the CSS style disappears Index.php代替Index.html,如何访问,我显然必须更改的.htaccess文件在哪里? - Index.php instead Index.html, how to access, and where is the .htaccess file that i have apparently have to change? 如何从index.html在私有文件夹中运行index.php - How do I run index.php in a private folder from index.html 如何使用Shell脚本使&#39;index.php&#39;的优先级高于&#39;index.html&#39;的优先级 - How do i make 'index.php' higher priority than 'index.html' with a Shell script 服务器页面在index.php或index.html之间的优先级 - Server page priorities between index.php or index.html 将默认页面从index.php更改为index.html - Change default page from index.php to index.html 使用index.php而不是index.html是不好的做法吗? - Is it bad practice to use index.php instead of index.html? 何时使用 index.php 而不是 index.html - when to use index.php instead of index.html 隐藏URL的index.php(或index.html) - Hide index.php (or index.html) of an URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM