【问题标题】:Angular 6 build failed to load resourcesAngular 6 构建无法加载资源
【发布时间】:2019-05-10 22:11:54
【问题描述】:

我正在尝试使用命令 ng build --prod 构建我的应用程序,但是当我尝试在 dist 文件夹中加载 index.html 文件时,我收到以下错误:

styles.76a1111….css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
runtime.06daa30….js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
polyfills.f6ae3e8….js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
scripts.f11023c….js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
main.4ee5dbf….js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
styles.76a1111….css:1 Failed to load resource: the server responded with a status of 404 (Not Found)

index.html 文件中,我有以下内容:

<base href="/">

angular.json 文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "digital": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/digital",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "./node_modules/bootstrap/dist/css/bootstrap.css",
              "src/styles.scss"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.js",
              "./node_modules/bootstrap/dist/js/bootstrap.bundle.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "digital:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "digital:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "digital:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "wedigital-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "digital:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "digital:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "digital",
  "schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
  }
}

其他信息:

Angular CLI: 6.2.7
Node: 8.10.0
OS: darwin x64
Angular: 6.1.10

dist 文件夹中的文件

3rdpartylicenses.txt
assets
favicon.ico
index.html
main.4ee5dbfc414143867b55.js
polyfills.f6ae3e8b63939c618130.js
runtime.06daa30a2963fa413676.js
scripts.f11023cbae55e24a4eeb.js
styles.76a1111b5a6844e3fc2b.css

【问题讨论】:

  • ng build --prod --verbose 的输出是什么?
  • 上述命令给出相同的输出和错误
  • dist 文件夹中有哪些文件?你编辑了index.html的内容吗?
  • 我拥有的文件在问题的上方(我已经更新了它),不,我没有编辑 index.html?
  • verbose 应该有更多的细节。确保它作为正常构建有更多的输出并查看这个并尝试使用--progress 标志来逐步显示出了什么问题

标签: angular build


【解决方案1】:

您需要将&lt;base href="/"&gt; 更改为&lt;base href="./"&gt;

【讨论】:

  • 或者只是删除它,因为那是隐含的base
  • 谢谢伙计。这是正确的答案,应该用绿色勾号标记为正确!!!
【解决方案2】:

您无法直接从dist 文件夹打开index.html。您必须使用某种反向代理服务器。

您可以使用Nginx

在您的nginx.conf 文件中修改并添加以下配置:

location /angular {
   alias "C:\\<Your Project Location>\\dist";   
   index index.html index.htm;
   try_files $uri $uri/ /index.html =404;
}

然后您可以通过以下方式访问您的项目 dist 文件:
http://localhost/angular

你的错误也将得到解决。

假设您的angular.json 文件正确定义了baseHrefdeployUrl
示例:

{
  ...
  "projects": {
    "<Your project>": {
      ...
      "architect": {
        "build": {
          ...
          "options": {
            "baseHref": "/angular/",
            "deployUrl": "/angular/",
            ...
          }
        }
      }
    }
  }
}

【讨论】:

    【解决方案3】:

    试试下面的说明,

    1. 使用 AppModule 配置路由器

      RouterModule.forRoot(appRoutes, { useHash: true })

    2. 从 index.html 中移除 base URL 并在 AppModule 中动态添加

      import { APP_BASE_HREF } from '@angular/common'; providers: [{ provide: APP_BASE_HREF, useValue: '/' }],

    【讨论】:

    • 这会带来 10 个错误:GET localhost:4200/policy/runtime.js net::ERR_ABORTED 404 (Not Found)
    • 您是否从 index.html 中删除了 &lt;base href="/"&gt;
    • 是的,我确实这样做了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2018-11-23
    • 2018-04-08
    • 2013-12-20
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多