2

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

我得到 的错误在此处输入图像描述

在 chrome 开发者工具的源选项卡中,没有包含 资产文件夹

在此处输入图像描述

这是我的角度 cli 版本 在此处输入图像描述

我试过这个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"
}
4

3 回答 3

5

我也有同样的问题

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

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

C:\angular-projects(1)\angularapp

然后将其更改为

C:\angularprojects1\angularapp
于 2018-09-13T03:55:08.147 回答
0

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

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

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

于 2018-07-26T07:21:00.477 回答
-2

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

比如你的项目在E盘,

更改此位置

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

E:\Data\project1\Angularlearning\app

然后,您将能够在 chrome 开发人员工具的源选项卡下看到加载到浏览器的资产文件夹。

于 2018-09-16T06:12:37.067 回答