Angular 6 assets文件夹未在浏览器上呈现

问题描述 投票:0回答:3

我正在学习角度,我正在尝试将assets文件夹中的本地图像加载到main.component.html中并且我已正确设置路径,而我在其打开图像的路径上执行alt +单击。但是图像没有加载到浏览器上。

错误我得到enter image description here

在chrome developer工具的sources选项卡中,没有包含assets文件夹

enter image description here

这是我的角度cli版本enter image description here

我试过这个https://github.com/angular/angular-cli/issues/5268但没有用,有人请帮忙这是我的angular.json文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "game-of-thrones": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/game-of-thrones",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/font-awesome/css/font-awesome.css"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/bootstrap/dist/js/bootstrap.min.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": "game-of-thrones:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "game-of-thrones:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "game-of-thrones: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": [
              "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/**"
            ]
          }
        }
      }
    },
    "game-of-thrones-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "game-of-thrones:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "game-of-thrones"
}
angular http-status-code-404 assets file-not-found
3个回答
4
投票

我也有同样的问题

GET http://localhost:4200/assets/img/img.png 404 (Not Found)

但是我通过删除项目路径中的所有特殊字符('(','@','$'等)来修复它。例如,如果你有一个类似的项目路径

C:\angular-projects(1)\angularapp

然后改为

C:\angularprojects1\angularapp

0
投票

通常,您需要在index.html中为您的基本href添加前缀,如下所示:

base href="/game-of-thrones/"

为了在正确的文件夹中查找资产。


-2
投票

通过删除所有特殊字符,符号到应用程序的文件夹位置,找到解决上述问题的方法,看看这个Github issue

例如,如果您的项目是在E盘中,

改变这个位置

E:\Data\project - 1\Angular(learning)\app

E:\Data\project1\Angularlearning\app

然后,您将能够在chrome developer tools的sources选项卡下看到已加载的assets文件夹到浏览器。

© www.soinside.com 2019 - 2024. All rights reserved.