如何修复 Angular ng build 不生成 index.html
除非你的构建有错误(从 ng build 输出可以清楚看到),ng build 不生成 index.html 的原因是生成的包超过了最大允许大小。
从输出中可以明显看出
ng_build_error.txt
Error: bundle initial exceeded maximum budget. Budget 1.00 MB was not met by 3.73 kB with a total of 1.00 MB.ng_build_error.txt
Error: bundle initial exceeded maximum budget. Budget 1.00 MB was not met by 3.73 kB with a total of 1.00 MB.要修复它,要么通过删除不需要的功能或库或拆分某些功能到额外模块来缩小你的应用程序,或者至少暂时增加允许的预算。
为此,打开 angular.json 并查找 "budgets": {... 部分:
angular_json_budgets.json
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],angular_json_budgets.json
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],增加 "type": "initial"(上面列表中的第一个条目)的 maximumError 值。
例如,你可以将其从 1mb 增加到 4mb 来修复你的构建。
之后,再次运行 ng build 命令,index.html 应该会生成。
但请注意,拥有巨大的 JS 包确实会减慢你的 Web 应用程序,特别是对于移动用户。
Check out similar posts by category:
Angular
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow