Angular - 如何在运行 ng-build 命令时动态更改 index.html 文件中的内容

2023-12-23

我想在运行时更改脚本标记的 URLng build命令与--prod选项和没有--prod选项。下面是一个例子。

ng 构建 --prod

<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
    <script src="https://my-site.net/prod.js"></script>
</body>
</html>

ng build

<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
    <script src="https://my-site.net/dev.js"></script>
</body>
</html>

我怎样才能实现这个目标?


In angular.json您可以配置要使用的索引文件(我使用的是 Angular 8):

"projects": {
    "projectName": {
        "architect": {
            "build": {
                "options": {
                    "index": "src/index.html", // this is the default location (also for ng serve)
                    "configurations": {
                        "production": {
                            "index": "index-prod.html" // this is the prod version (you can also link it to /prod/index.html or whatever: it will be placed in the dist root folder with the provided name)
                        },
                        "development": {
                            "index": "index-whatever.html" // this is the version for ng build --configuration=development`
                        }
                    }
                }
            }/*, UPDATED: THIS DOESN'T WORK
            "serve": {
                "index": "dev/index.html" // file to be used in ng serve
            }*/
        }
    }
}

据我所知,这些都不是"index"s 是强制性的,除了第一个

旁注:这是我刚刚尝试过的一些尝试、构建和ng serves。我在任何文档中都没有找到它。

检查一下并告诉我。

===========

2019 年 10 月 19 日更新: 我注意到上面报告的配置仍然使用src/index.html也为了serve on "@angular/cli": "^8.3.6", "@angular/compiler-cli": "^8.2.8"。看起来没有覆盖serve本身。 我的问题是:should serve总是继承基础index配置?

尝试进一步挖掘,我发现了这一点:https://github.com/angular/angular-cli/issues/14599 https://github.com/angular/angular-cli/issues/14599

有一个示例说明它如何与新 CLI 一起工作,但尝试替换

"development": {
    index": "index-whatever.html"
}

with a

"development": {
    index": {
        "input": "index-whatever.html"
    }
}

和建筑,它给了我一个Schema validation failed with the following errors: Data path ".index" should be string.

===========

2020 年 4 月 10 日更新:

我刚刚注意到,在十月的更新中,我错过了最新代码片段中的引用。我不记得它是否是直接从我的代码复制的 - 因此报告的错误可能与此相关 - 或者当我尝试重写代码时这是一个错误。我一有时间就会再检查一次

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular - 如何在运行 ng-build 命令时动态更改 index.html 文件中的内容 的相关文章

随机推荐